14

As far as I know, everything is object in Python and the id() should (am I right?) return a different number for each object.

In my case, id(1) returns 4298178968, id(2) returns 4298178944 but I get the same values for all float types, id(1.1) returns 4298189032, id(2.2) also returns 4298189032 and more.

Why I get the same id for all float values?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
ehsandotnet
  • 1,050
  • 3
  • 16
  • 26

2 Answers2

22

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.

Community
  • 1
  • 1
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • Ah yes, I was in ipython where the numbers are stored in Out! :) – Andy Hayden Jun 16 '13 at 10:03
  • Note that this interning of the small integers is probably a feature of the CPython _implementation_ rather than a guaranteed property of the language. In this case, it is similar to the small NSInteger values (0 through 12 from memory) in some Objective-C implementations where people have wondered about the strange retain counts. And, like that, it's probably not a good idea to rely on this interning. – paxdiablo Jun 16 '13 at 10:14
  • 1
    @paxdiablo: the whole meaning of `id()` is a CPython implementation detail. – Martijn Pieters Jun 16 '13 at 10:17
5
>>> id(1.1)
154154684

As 1.1 was not assigned to any variable so it is garbage collected and next time same id is going to be used for a float:

>>> id(2.2) 
154154684

save 1.1 in a variable:

>>> f = 1.1
>>> id(f)
154154684  #this id is locked for now

Now new address is used:

>>> id(1.1)
154154700
>>> id(2.2)
154154700

This applies to integers as well:

>>> id(260)
154302180
>>> id(280)
154302180

Integers from -5 to 256 are actually cached in python, so they are always going to return different IDs.( "is" operator behaves unexpectedly with integers )

For strings:

Like integers some strings are also cached in python. So, id of such strings is going to be different(For details read : 'is' operator behaves differently when comparing strings with spaces ):

>>> id('foo')
162861592
>>> id('foo')
162861568

Non-alphanumeric string(uses same id):

>>> id('foo!&9((&')
162840000
>>> id('foo!&9((&')
162840000
Community
  • 1
  • 1
Ashwini Chaudhary
  • 244,495
  • 58
  • 464
  • 504