与==进行比较时,具有相同ID的对象是否总是相等? [英] Are objects with the same id always equal when comparing them with ==?

查看:73
本文介绍了与==进行比较时,具有相同ID的对象是否总是相等?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有两个对象o1和o2,并且我们知道

If I have two objects o1 and o2, and we know that

id(o1) == id(o2)

返回 true

然后,是否遵循

o1 == o2

还是不是总是这样?我正在研究的论文说并非如此,但是我认为应该是这样!

Or is this not always the case? The paper I'm working on says this is not the case, but in my opinion it should be true!

推荐答案

并非总是如此:

>>> nan = float('nan')
>>> nan is nan
True

或用与问题相同的方式表达:

or formulated the same way as in the question:

>>> id(nan) == id(nan)
True

但是

>>> nan == nan
False

NaN 很奇怪。根据定义,它不等于或小于或大于其自身。但这是同一对象。更多详细信息,为什么所有比较都必须在 False 所有比较返回false-ieie754-nan-values>此SO问题

NaN is a strange thing. Per definition it is not equal nor less or greater than itself. But it is the same object. More details why all comparisons have to return False in this SO question.

这篇关于与==进行比较时,具有相同ID的对象是否总是相等?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆