3

Possible Duplicate:
Python β€œis” operator behaves unexpectedly with integers

>>>a=123
>>>b=123
>>>a is b
True
>>>id(a)==id(b)
True
My question is, why is id(a) the same as id(b)?.
Aren't they two different instances of class int?
Community
  • 1
  • 1
asdfg
  • 2,541
  • 2
  • 26
  • 25

3 Answers3

3

Usually small integers reference the same cached object in memory for efficiency purposes.

jackbot
  • 2,931
  • 3
  • 27
  • 35
2

ints are cached. That is an implementation detail that shouldn't matter since ints are immutable anyway.

nosklo
  • 217,122
  • 57
  • 293
  • 297
1

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

m1k3y3
  • 2,762
  • 8
  • 39
  • 68