1

From Python3 console:

$ python3
Python 3.4.2 (default, Oct 19 2014, 17:55:38) 
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.54)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> a = 1.23
>>> b = a
>>> id(a)
4457705664
>>> id(b)
4457705664
>>> b = 5 # new object assigned to b
>>> id(b)
4456877312
>>> a = 5 # new object assigned to a?
>>> id(a)
4456877312
>>> a = 1.23 # should be a new object?
>>> id(a)
4457705664

So, my question here is hopefully pretty obvious. The ID of a changes when it is assigned to a new value, but when I reassign the value 1.23, the ID actually changes back to what it was originally. How does that work? My assumption is that once the object 1.23 had lost its references, it would be GC'd. Is this some kind of interpreter optimization or something?

Evan Zamir
  • 8,059
  • 14
  • 56
  • 83
  • This question is not really a duplicate, as far as I can tell. The question you refer to concerns two *different* floats being assigned the same id. This question talks about the same float not being garbage collected. – Pascal Bugnion Dec 29 '14 at 20:10
  • As far as I can tell, this is an implementation detail of the garbage collector. There is nothing forcing the garbage collector to run. If you interleave `import gc ; gc.collect()` after having re-assigned `a`, the float `1.23` gets garbage collected. Interestingly, the behaviour is different in python 2.7. – Pascal Bugnion Dec 29 '14 at 20:13
  • Thanks @PascalBugnion! That is what I was curious about. – Evan Zamir Dec 29 '14 at 20:25

0 Answers0