Same value for id(float)

Python can reuse memory positions.

When you run:

id(1.1)

you create a float value, ask for its id(), and then Python deletes the value again because nothing refers to it. When you then create another float value, Python can reuse the same memory position and thus id(2.2) is likely to return the same value for id():

>>> id(1.1)
140550721129024
>>> id(2.2)
140550721129024

Do this instead:

float_one, float_two = 1.1, 2.2
print id(float_one), id(float_two)

Now the float values have references to them (the two variables) and won’t be destroyed, and they now have different memory positions and thus id() values.

The reason you see different id() values for small integers (from -5 through to 256) is because these values are interned; Python only creates one 1 integer object and re-uses it over and over again. As a result, these integers all have a unique memory address regardles, as the Python interpreter itself already refers to them, and won’t delete them until the interpreter exits.

Leave a Comment