Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: object identity question? [duplicate]

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?
like image 599
asdfg Avatar asked Jan 23 '26 04:01

asdfg


2 Answers

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

like image 184
jackbot Avatar answered Jan 25 '26 18:01

jackbot


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

like image 44
nosklo Avatar answered Jan 25 '26 17:01

nosklo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!