您需要添加2种方法,注意
__hash__和
__eq__:
class MyThing: def __init__(self,name,location,length): self.name = name self.location = location self.length = length def __hash__(self): return hash((self.name, self.location)) def __eq__(self, other): return (self.name, self.location) == (other.name, other.location) def __ne__(self, other): # Not strictly necessary, but to avoid having both x==y and x!=y # True at the same time return not(self == other)
Python dict文档对关键对象定义了这些要求,即它们必须是可哈希的。



