Python assigning variables pointing to the same object, when might one need this? Examples? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Python assigning variables pointing to the same object, when might one need this? Examples?

We (I) have learned in the link below that sometimes, when assigning variables, python refers to the same object, instead of creating a copy of it. I could not find any practical usage for this. Can you show some examples when this behavior is useful? Or what is the practical benefit of this? Thanks. https://www.sololearn.com/Discuss/1178960/?ref=app

31st Mar 2018, 4:56 AM
Edward
Edward - avatar
1 Answer
+ 1
Yes, it is many times not useful to have two variables that point towarda the same object. But, that is how python works. It does not create new objects unless given in direct value. This helps to save memory. In practical coding it sometimes helps if you want to use one of those variables for another object. This could be helpful in loops especially. You can have multiple pointers towards the same object and so can you destroy them one by one or all at once. But, yeah it is slightly unintuitive that we can't directly create copies of the objects by assignment.
31st Mar 2018, 5:25 AM
Kaustubh Patil
Kaustubh Patil - avatar