两个Python对象如何具有相同的ID,但'is'运算符返回False? [英] How can two Python objects have same id but 'is' operator returns False?
本文介绍了两个Python对象如何具有相同的ID,但'is'运算符返回False?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
id(t+t), id(t*2)
(42838592, 42838592)
(t+t) is (t*2)
False
如果两个变量指向同一个对象,则'is'运算符将返回true.但是第一行表示两者具有相同的id,但是'is'运算符给出了错误的值.
If two variable point to same object 'is' operator will return true.But first line said both have same id but 'is' operator give false value.
推荐答案
在第一个示例中,您的对象在时间上没有重叠:创建一个对象,然后销毁它们,然后创建另一个具有相同ID的对象.
In the first example, your objects don't overlap in time: one is created then destroyed, then another is created with the same id.
将它们与is
进行比较时,您将同时按住两个对象,因此它们会获得不同的ID.
When you compare them with is
, you are holding onto both objects, so they get different ids.
这篇关于两个Python对象如何具有相同的ID,但'is'运算符返回False?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文