<分区>
Possible Duplicate:
Python “is” operator behaves unexpectedly with integers
>>>a=123<br/>>>>b=123<br/>>>>a is b<br/>True<br/>>>>id(a)==id(b)<br/>True
我的问题是,为什么 id(a) 与 id(b) 相同?它们不是 class int 的两个不同实例吗?
<分区>
Possible Duplicate:
Python “is” operator behaves unexpectedly with integers
>>>a=123<br/>>>>b=123<br/>>>>a is b<br/>True<br/>>>>id(a)==id(b)<br/>True
我的问题是,为什么 id(a) 与 id(b) 相同?最佳答案
通常,为了提高效率,小整数会引用内存中的相同缓存对象。
关于Python:对象身份问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2419701/