Python:对象身份问题?
可能的重复:
Python“is”运算符对整数的行为异常
>>>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?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
通常,出于效率目的,小整数会引用内存中相同的缓存对象。
Usually small integers reference the same cached object in memory for efficiency purposes.
int
被缓存。这是一个无关紧要的实现细节,因为int
无论如何都是不可变的。int
s are cached. That is an implementation detail that shouldn't matter sinceint
s are immutable anyway.变量
都是对对象 123 的引用,对象 123 的 id 是唯一的。
当您将相同的值123分配给两个差异变量a和b时,
则将相同的对象123分配给这两个变量变量a和b,但对该对象的引用计数在您的情况下增加,对象123的引用计数为二
variables
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