class test: def __init__(self,name): self.name = name obj1 = test('a') obj2 = test('a') hash1 = magicHash(obj1) hash2 = magicHash(obj2)
我正在寻找的是hash1 == hash2的东西. python中是否存在类似的东西?我知道我可以测试obj1.name == obj2.name,但是我正在寻找一些可以在任何对象上使用的东西.
__hash__
class test: def __init__(self,name): self.name = name def __hash__(self): return hash(self.name) >>> hash(test(10)) == hash(test(20)) False >>> hash(test(10)) == hash(test(10)) True