+ 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
1 Antwort
+ 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.