特殊的工作__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
, 0
or 1
, 表示它低于、等于或高于other
. 返回False
实际上会使其与所有内容进行比较,因为 is 的整False
数值0
。
class Test():
def __cmp__(self, other):
return -1
另请注意,__cmp__
它在 Python 3 中已被弃用并被忽略。您应该实现__eq__
其他所谓的富比较运算符。