我只想找出这些结果背后的逻辑:
>>>nan = float('nan')
>>>nan == nan
False
# I understand that this is because the __eq__ method is defined this way
>>>nan in [nan]
True
# This is because the __contains__ method for list is defined to compare the identity first then the content?
但是在这两种情况下,我认为幕后调用的函数是PyObject_RichCompareBool
,对吗?为什么会有差异?它们不应该有相同的行为吗?
nan
在所有编程语言中都是这样的(来自标准),对于第二个我不确定。 - simonzack__contains__
可能会短路,因为nan is nan == True
。此外,float('nan') in [float('nan')] == False
。 - lunixbochs