Possible Duplicate:
Python “is” operator behaves unexpectedly with integers
>>>a=123>>>b=123>>>a is bTrue>>>id(a)==id(b)True
My question is, why is id(a) the same as id(b)?. Aren't they two different instanc开发者_运维技巧es of class int?Usually small integers reference the same cached object in memory for efficiency purposes.
int
s are cached. That is an implementation detail that shouldn't matter since int
s are immutable anyway.
variables
a and b
both are references to the object 123 whose id is unique.
when u assign same value 123 to two diff variables a and b,
then same object 123 is assigned to both variables a and b but reference count made to that object increases in your case refrecnce count for object 123 is two
精彩评论