object() == object() 保证为 False 吗?
假设我创建了两个 object
类的实例。这两个实例是否保证彼此不相等?换句话说,object() == object()
是否保证为 False
,还是依赖于实现?
我知道 object() is object()
保证为 False
,但在这里我问的是 object() == object()
>。
Suppose I create two instances of class object
. Are these two instances guaranteed to be not equal to each other? In other words, is object() == object()
guaranteed to be False
, or is it implementation-dependent?
I understand that object() is object()
is guaranteed to be False
, but here I am asking about object() == object()
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,可以保证
object() == object()
为False
因为它是 记录“默认情况下,object
通过使用实现
”。__eq__()
Yes it is guaranteed that
object() == object()
isFalse
because it is documented that "by default,object
implements__eq__()
by usingis
".每个 Python 3.x 版本都旨在向后兼容以前的 3.x 版本。由于它是基类,因此更改
object
的比较行为将破坏这一向后兼容性承诺。所以我认为你可以依赖它,但如果我们知道你想要做什么需要这个不变量,我们可能会提供更好的建议。您始终可以使用
assert
来确保它仍然为真。Each Python 3.x release is intended to be backward-compatible with previous 3.x releases. As it's the base class, changing the comparison behavior of
object
would break this backward compatibility promise. So I think you can rely on it, but if we knew what you're trying to do that requires this invariant, we could probably give better advice.You can always use
assert
to make sure it continues to be true.