Python中x == x是否为假?

ast*_*nlu 8 python floating-point equality

我在统计模块中偶然发现了SciPy源代码中的这行代码:

return 1.0*(x==x)
Run Code Online (Sandbox Code Playgroud)

这是否会归还其他东西1.0?换句话说,是否有X使得任何价值x == x持有False

use*_*136 22

根据IEEE 754标准,无论数字是什么,非数字(NaN)必须始终比较错误.

Python 2.7.2+ (default, Oct  4 2011, 20:06:09) 
[GCC 4.6.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> x=float("NaN")
>>> x==x
False
Run Code Online (Sandbox Code Playgroud)


Jam*_*mes 9

用户定义的类型可以覆盖相等运算符以执行任何操作:

Python 3.2.2 (default, Feb 10 2012, 09:23:17) 
[GCC 4.4.5 20110214 (Red Hat 4.4.5-6)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> class A:
...     def __eq__(self, other):
...         return False
... 
>>> x=A()
>>> x==x
False
Run Code Online (Sandbox Code Playgroud)