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,341
  • 2
  • 25
  • 24

3 Answers3

3

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

jackbot
  • 2,871
  • 3
  • 26
  • 35
2

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

nosklo
  • 205,639
  • 55
  • 286
  • 290
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,610
  • 8
  • 38
  • 68