Is it useless to copy a String in Python?? (5 Options)

preview_player
Показать описание
A string is immutable, so let's look at 5 different ways to copy a string and see if it creates an actual copy.

📓 ML Notebooks available on Patreon:

If you enjoyed this video, please subscribe to the channel:

~~~~~~~~~~~~~~~ CONNECT ~~~~~~~~~~~~~~~

~~~~~~~~~~~~~~ SUPPORT ME ~~~~~~~~~~~~~~

#Python #Shorts
Рекомендации по теме
Комментарии
Автор

There’s a function called copy.deepcopy() for this very purpose but the creator is being cheeky it seems and is using copy.copy() which the documentation clearly says only provides a shallow copy.

Edit: Even if you do a shallow copy with copy.copy(), then change string a or b, it does not affect the other. Strings are immutable in Python. The ID of the strings WILL stay the same until a change is made to them. They behave like different objects though because of immutability.

TLDR: Use copy and you won’t have to think more about it until you’re dealing with memory IDs explicitly. Python strings will just work as you expect them to.

swaggitypigfig
Автор

Since strings are immutable, there is no reason to copy it. Copying it will just create another immutable string. Now u have two same strings which cannot be modified, which is a waste of memory.

kaustuvray
Автор

Actually it's good optimization to keep one string in memory, than storing two duplicate of them

And later I believe if you change the string it will clone it and place the new one in different location
Actually it does same for anything else even without direct assignment
Like try
A="h"
B="h"

Then check the IDs
It will be same
most modern languages does that automatically for you

mido__gm
Автор

The reason the last one works is because when you first reverse the string, you now have a different string, so it has to give it a different place in memory. And then you reverse it again, and that new string is again different from the reversed string, so it has to make a new copy again.

You could try copy.deepcopy, I saw a comment here mention that then someone else say it didnt work, I have not tried it.

You could also make your own function to do it in a loop, one character at a time, or you could try list comprehension.

legitjimmyjaylight
Автор

Python has String pool built in
So, every time you create new string — python looks into that pool, and if python finds that string in pool of strings — it refers to it, so even
a = “Hello”
b = “Hello”

gives you same ids id(a) == id(b)

hihi-hehe
Автор

Ok But it is for optimization. It will be wastage of memory if you will have two strings with same content.
In PYTHON As soon as you will change content of any string, its id will be changed.
Even if you will assign two stirngs separately with same content, their id will be same
a = "asd"
b = "asd"
both of them will have same id

shail
Автор

this behavior is actually what saves you from having new variables each time you call a function with some arguments!
the variable will only actually copy in memmory when you try to make some assignment to it(edit it)

Shayan
Автор

Anybody care to explain WHY? the last method produces a different id?

siddharthyadav
Автор

The fact that you explicitly use a copy module and yet it doesn't copy it is astounding 😂

flamendless
Автор

Legends say he is still getting the same id😂

abhirammadhu
Автор

Well, I’m glad Python takes care of that for me

VkToo
Автор

You only reference that string in a place in the memory, if you make a change, only then will a new ID be made and a new string in memory. Its for preserving space in memory. Watch a memory managment tutorial. I watched Java memory managment on linkedin by Frank P Moley. I understand now

harunjasarevic
Автор

i think you forgot to set "deep=True" in copy()

abir
Автор

Missy Elliot knew about "flip it and reverse it".

harrysc
Автор

First example. What do you mean by “this is the same Id so it’s not an actual copy.”?

txreal
Автор

Construct it based on all the indexed chars in the original string?

blazingazong
Автор

it hard to make deepcopy of string in python
it gives same id becouse of memory management but you change another copy string it not afftect the main string

mehul
Автор

There are so many reaction in this comment section about deepcopy from people that don't know their stuff plus are lazy enough to not even try it.

victorsavenije
Автор

lmao, that last one was so inefficient.

charlemagnejavinal
Автор

Why I need the same string with different ID ? Can someone tell me please

ivv