为什么“对象”的两个实例是尽管它们的“id()”被认为是不相同的。是一样的吗?
根据 Python 语言参考:
“
is
”运算符比较两个对象的身份;id()
函数返回一个表示其身份的整数。CPython 实现细节:对于 CPython,
id(x)
是存储x
的内存地址。
但是:
>>> object() is object()
False
>>> id(object()) == id(object())
True
这与记录的行为如何一致?尽管两个对象的 id()
相同,但为什么它们被认为不相同?为什么 object() is object()
被认为是 False
,尽管 id(object()) == id(object())
是 >真的
吗?
According to the Python Language Reference:
The ‘
is
’ operator compares the identity of two objects; theid()
function returns an integer representing its identity.CPython implementation detail: For CPython,
id(x)
is the memory address wherex
is stored.
However:
>>> object() is object()
False
>>> id(object()) == id(object())
True
How is this consistent with the documented behavior? Why are the two objects considered non-identical although their id()
are the same? Why is object() is object()
considered False
although id(object()) == id(object())
is True
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论