文档声明 x==y 并不意味着 x!=y,这是真的吗?
Docs state that x==y does not imply x!=y, is this true?
如问题所述,python 文档陈述了一条相当矛盾的路线。有问题的行可以在 this page.
上找到
有问题的那一行相当直截了当地说:
"There are no implied relationships among the comparison operators. The truth of x==y does not imply that x!=y is false."
Python 中是否有满足此要求的声明?或者它只限于复杂的陷阱代码。
下面是一个 == 和 != 都为 True 的例子
class MyClass:
def __init__(self):
pass
def __eq__(self, other):
return True
def __ne__(self, other):
return True
b1 = MyClass()
b2 = MyClass()
print b1 == b2
print b1 != b2
以上两行都将打印 True
如问题所述,python 文档陈述了一条相当矛盾的路线。有问题的行可以在 this page.
上找到有问题的那一行相当直截了当地说:
"There are no implied relationships among the comparison operators. The truth of x==y does not imply that x!=y is false."
Python 中是否有满足此要求的声明?或者它只限于复杂的陷阱代码。
下面是一个 == 和 != 都为 True 的例子
class MyClass:
def __init__(self):
pass
def __eq__(self, other):
return True
def __ne__(self, other):
return True
b1 = MyClass()
b2 = MyClass()
print b1 == b2
print b1 != b2
以上两行都将打印 True