您也需要定义
__hash__。例如
class A(object): def __hash__(self): print '__hash__' return 42 def __cmp__(self, other): print '__cmp__' return object.__cmp__(self, other) def __eq__(self, rhs): print '__eq__' return Truea1 = A()a2 = A()print a1 in set([a1])print a1 in set([a2])
将按预期工作。
作为一般规则,你实现任何时间
__cmp__,你应该实施
__hash__,使得所有
x与
y这样
x== y,
x.__hash__() == y.__hash__()。



