特殊的工作__cmp__不起作用。说下面的代码:
class Test():
def __cmp__(self, other):
return False
t1 = Test()
t2 = t1
print t2 == t1
我应该得到 False,因为cmp总是返回 False。但实际上,python 正在为我打印 True。
有什么建议吗?
特殊的工作__cmp__不起作用。说下面的代码:
class Test():
def __cmp__(self, other):
return False
t1 = Test()
t2 = t1
print t2 == t1
我应该得到 False,因为cmp总是返回 False。但实际上,python 正在为我打印 True。
有什么建议吗?
__cmp__应该返回-1, 0or 1, 表示它低于、等于或高于other. 返回False实际上会使其与所有内容进行比较,因为 is 的整False数值0。
class Test():
def __cmp__(self, other):
return -1
另请注意,__cmp__它在 Python 3 中已被弃用并被忽略。您应该实现__eq__其他所谓的富比较运算符。