开发者

Python: object identity question? [duplicate]

开发者 https://www.devze.com 2022-12-22 08:02 出处:网络
This question already has answers here: Closed 10 years ago. Possible Duplicate: Python “is” operator behaves unexpectedly with integers
This question already has answers here: Closed 10 years ago.

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 instanc开发者_运维技巧es of class int?


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


ints are cached. That is an implementation detail that shouldn't matter since ints 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

0

精彩评论

暂无评论...
验证码 换一张
取 消