"is" and "id" in Python 3.5

I have questions: I am using python 3.5 system, win7-32bit.

Here is my code:

a=3 b=3 print(id(a)) print(id(b)) 

it returns:

 1678268160 1678268160 

So, we could know that a and b refer to the same object.

But here is the question:

 a=3 b=3 print( id(a) is id(b) ) 

It returns:

 False 

I do not understand why this happened, I think it should be true. Can someone explain to me? Thanks!

0
python
source share
2 answers

id returns a Python int object (the memory address of the id -ing object, although this is an implementation detail). But besides a very small int (again, implementation details), Python does not have int caching; if you compute the same int two ways, these are two different int objects that have the same value. Similarly, each time id is called, a new int is created, even if the objects are the same.

The equivalence for id and is is that a is b means id(a) == id(b) , not id(a) is id(b) (and in fact, since id are large numbers, id(a) is id(b) almost always False ).

Also note that your test case is corrupted in other ways:

 a = 3 b = 3 a is b 

returns True for comparison is due to the small int cache in CPython; if you did:

 a = 1000 b = 1000 a is b 

a is b will be False ; your identity assumptions are only saved in CPython for numbers ranging from -5 to 256 inclusive, which are singles for performance reasons, but all other ints are recreated as needed, not single ones.

+1
source share

id(a) is id(b) compares the identifiers of identifiers returned by id .

id(a) == id(b) will be True with a is b and during the lifetime of the object, the value returned by id will always be the same. However, every time id is called, an integer (the same value) is returned, so id(a) is id(b) False .

+1
source share

All Articles