There are several things in the x=5 instruction:
- an int object with a value of
5 (or found if it already exists); - the name
x is created (or disassociated with the last marked "x"); - the reference counter for a new (or found) int object is incremented by 1;
- name
x is associated with an object with a value of '5' created (or found).
Because int objects are immutable, they can be interned to increase efficiency. String objects are more likely to be interned.
Here are some examples:
>>> x=5 # discussed >>> id(x) # the 'id' which in cPython is the memory address. 140246146681256 >>> y=x # now two names, 'x' and 'y' associated with that object >>> id(y) 140246146681256 # same object >>> z=5 # no guaranteed, likely the same object referred to by 'x' and 'y' >>> id(z) 140246146681256 # id is the same! The object labeled 'x' was found and labeled 'z' >>> del x # ref count to object '140246146681256' decreased by 1 >>> del y # same >>> z 5 >>> id(z) 140246146681256 # same object but the names ''x' and 'y' no longer label it
The best way to think about variable names, such as 'x' at x=5 , is to label.
It is possible that there are objects without a label, such as 5 separate string objects created in this understanding of the list:
>>> li=[str(x) for x in range(5)] >>> li ['0', '1', '2', '3', '4']
Then you can create objects that match the value of the same 5 objects independently:
>>> li2=list('012345') # longer list; completely different construction >>> li2 ['0', '1', '2', '3', '4', '5']
You can get your individual memory addresses (in cPython) or a unique id address:
>>> [id(x) for x in li] [4373138488, 4372558792, 4372696960, 4373139288, 4373139368] >>> [id(x) for x in li2] [4373138488, 4372558792, 4372696960, 4373139288, 4373139368, 4372696720]
Note that the two independently created lists of the anonymous object are the same (for the first 5 in this case). I intentionally used strings because they are more likely to be buried ...
Think of it this way: two processes happen with x=5 :
- an object is created (or found if it is immutable, interned and exists) and
- object is marked.