You create a new object without any other references , and Python reuses the memory location when the object is destroyed again after id() with it. In CPython, the result of id() is the memory location of the object. From the id() of the function documentation :
CPython implementation details . This is the address of the object in memory.
Line id(12345) creates a new instance of int() ; because it is bound as an id() argument, it has 1 reference to it. id() determines the location of the memory and returns this result. Upon return, the arguments are cleared, and the reference count to the int() instance drops to 0, and Python clears it. The memory is freed.
The next line creates a new instance of int() . There is memory in one place, so it is reused.
When instead of this you first bind a new object without a name, you created an additional reference to the object, and it is not cleared. The memory is not freed, and the new object will have to use the new memory address.
This part is also documented, again from the id() documentation:
This is an integer (or a long integer) that is guaranteed to be unique and constant for this object throughout its life. Two objects with non-overlapping lifetimes can have the same id() value.
Emphasis is mine.
When reconnecting ( x = [] when x already installed), Python first creates a new object , then refreshes x to point to this new object. This drops from the old list after creating a new list. This means that the old memory is still busy when a new list object is created.
To match this with specific steps:
- You create an object with
id() == 4301901696 . 4301901696 tied to x β The number of links for 4301901696 is 1.- You create an object with
id() == 4301729448 . 4301901696 not associated with x . The reference counter for 4301901696 drops to 0 and is cleared of memory.4301729448 tied to x . The number of links for 4301729448 is 1.- You create a new object,
4301901696 empty, so the new object gets id() == 4301901696 . 4301729448 not associated with x . The reference counter for 4301729448 drops to 0 and is cleared of memory.4301901696 tied to x . The number of links for 4301901696 is 1.- and etc.
This is also part of the documentation, the assignment assignment documentation tells you what the purpose of the order is in:
The assignment operator evaluates the list of expressions [...] and assigns a single result object to each of the target lists from left to right.
where the list of expressions is all that is to the right of the = symbol.