当实现具有多个属性的类时(如下面的玩具示例所示),处理散列的最佳方法是什么?
我猜__eq__
和__hash__
应该是一致的,但是如何实现一个能够处理所有属性的正确的散列函数呢?
class AClass:
def __init__(self):
self.a = None
self.b = None
def __eq__(self, other):
return other and self.a == other.a and self.b == other.b
def __ne__(self, other):
return not self.__eq__(other)
def __hash__(self):
return hash((self.a, self.b))
所以我想知道上面的例子是否合理。是吗?