Python non consistent Object-variable identity? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Python non consistent Object-variable identity?

Look at the code what happens: https://code.sololearn.com/cu6OcuKbp5Oj/#py

11th Jul 2018, 5:19 PM
Jose Berlines
Jose Berlines - avatar
3 Answers
+ 1
Simply because your variable is a reference to a list, when you assign to another variable they both have the same reference, so both refer to the same object. With integers you actually have a value assigned, so you can give the same value to other variables but they are separate entities edit: I'm not as familiar with how python deals with ints internally, perhaps this will help https://www.laurentluce.com/posts/python-integer-objects-implementation/
11th Jul 2018, 8:11 PM
Dan Walker
Dan Walker - avatar
+ 1
With integers the variable points to an integer value in memory, so the id changes whenever the value does, pointing to the new integer value. However other objects (lists included) keep pointing to the same object unless reassigned
11th Jul 2018, 9:54 PM
Dan Walker
Dan Walker - avatar
0
Still the behaviour is different when variables point to integers as when they point to lists.
11th Jul 2018, 9:31 PM
Jose Berlines
Jose Berlines - avatar