您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

在Python中,object()是否等于除自身之外的任何东西?

在Python中,object()是否等于除自身之外的任何东西?

object没有实现__eq__,因此回退到认比较id(x) == id(y),即 它们是同一对象实例x is y)吗?

每次调用都会创建一个新实例object()my_object它将永远不会*等于除自身以外的任何值。

这适用于2.x和3.x:

# 3.4.0
>>> object().__eq__(object())
NotImplemented

# 2.7.6
>>> object().__eq__(object())

Traceback (most recent call last):
  File "<pyshell#60>", line 1, in <module>
    object().__eq__(object())
AttributeError: 'object' object has no attribute '__eq__'
python 2022/1/1 18:34:43 有208人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶