为什么“对象”的两个实例是尽管它们的“id()”被认为是不相同的。是一样的吗?

发布于 2025-01-13 12:04:36 字数 643 浏览 1 评论 0原文

根据 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; the id() function returns an integer representing its identity.

CPython implementation detail: For CPython, id(x) is the memory address where x 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文