This question is motivated by this
this question which I both misread and
provided a botched answer (I deleted it)
I re-read http://docs.python.org/library/functions.html#id and just tried this in Python:
>>> a = 3
>>> id(a)
5392456
>>> a = 3
>>> id(a)
5392456
repeated a few times more …
The fact that these numbers (ie addresses of the object in memory) are
the same is implementation dependent, and not guaranteed, is that
correct? They could be different, right? My understanding is that each time I do
this simple assignment, I am creating a new object and binding it to a variable
identifier, so I can’t assume that they would be put in the same place
in memory.
Is this understanding correct? If so, are there any exceptions?
You can make that assumption but a times for immutable types like
intrather than create a new object, your variable might just reference the immutable object if it already exist. When you do an assignment you are creating a reference to an object. That object might be already around or might be created.