python – 在Dictionary键中对象不被认为是相同的 – 但实现了__
以下代码给出了一条错误消息:
class Test(object): def __init__(self,test = 0): self.test = test if __name__ == '__main__': t1 = Test(1) t2 = Test(2) t3 = Test(3) t4 = Test(1) my_dict = {} my_dict[t1] = 1 my_dict[t2] = 2 my_dict[t3] = 3 print(my_dict[t4]) Traceback (most recent call last): File "C:UsersAlexanderDocumentsVisual Studio 2015ProjectsPipeProcessTest PipeProcessTestDictionaryKeys.py",line 16,in <module> print(my_dict[t4]) KeyError: <__main__.Test object at 0x0000000002F18080> 这是因为python将t1和t4视为不同的对象.但是,当我使用以下代码实现比较运算符’eq’时: def __eq__(self,other): if self.test == other.test: return True else: return False 我收到另一条错误消息,“unhashable type:’Test’”告诉我现在字典不能散列Test对象.我该如何解决这个问题,以便Python能够识别t1和t4是否相同,还能够对Test对象进行哈希处理? 解决方法
这是因为python将t1和t4视为不同的对象.但是,当我使用以下代码实现比较运算符’eq’时:
当您在python中执行此操作时…. class Test(object): def __init__(self,test = 0): self.test = test if __name__ == '__main__': t1 = Test(1) t2 = Test(2) t3 = Test(3) t4 = Test(1) my_dict = {} my_dict[t1] = 1 my_dict[t2] = 2 my_dict[t3] = 3 这意味着您实际上正在尝试使用键作为Test对象创建一个dict,Python会首先检查键是否可以清洗. 当它返回obj .__ hash __()方法的任何整数值时,Python中的任何对象都是可散列的.在python中,默认情况下,所有用户定义的类都会获得一些id(self)的哈希值. 显然当你得到id值作为它的哈希值时,它们会看起来像这个值8772302607193.如果我们构造哈希表,它们可能看起来像这样的id. 让我们假设id是这样的…… id(t1) = 1 id(t2) = 4 # These are just assumptions. id(t3) = 10 # actual id's are long values. id(t4) = 20 这是哈希表的构造方式…. hash Actual value value ------------------ | 1 | t1 | ------------------ | 2 | NULL | ------------------ # Consider that you've respective values are | 3 | NULL | # here,just for representation I've mentioned ------------------ # t1,t2,t3,t4,.. | 4 | t2 | ------------------ | | ------------------ | 10 | t3 | ------------------ | SO ON 像这样你的哈希表被构造,所以这里当你尝试获取t4的值时只需要尝试my_dict [t4].首先通过调用t4 .__ hash __()来检查t4的哈希值,假设t4哈希值为20. 获取哈希值20后,它检查哈希表,索引为20,因为我们没有插入任何值,只是引发了KeyError异常,这就是你在尝试my_dict [t4]时得到KeyError的原因. 另一个场景: 如果您尝试覆盖__hash__方法f测试类并继续执行您在下面执行的相同操作. class Test(object): def __init__(self,test = 0): self.test = test def __hash__(self): self.test # You are just returning the same value if __name__ == '__main__': t1 = Test(1) t2 = Test(2) t3 = Test(3) t4 = Test(1) my_dict = {} my_dict[t1] = 1 my_dict[t2] = 2 my_dict[t3] = 3 由于我们重载了hash方法以返回与初始化相同的值,下面是我们得到的哈希值 t1 = 1,t2 = 2,t3 = 3,t4 = 1 当我们有多个具有相同哈希值的值时,这就是哈希表的样子. hash Actual value value ------------------ | 1 | [t1,t4] | # List of values for the same hash value. ------------------ | 2 | t2 | ------------------ # Consider that you've respective values are | 3 | t3 | # here,just for representation I've mentioned ------------------ # t1,... | 4 | NULL | ------------------ | SO ON 在这种情况下,当您尝试获取my_dict [t4]时,如前所述,首先检查哈希值t4 .__ hash __()返回1.现在Python dict检查哈希表中的索引1并获得多个值[t1,t4 ]. 这就是__eq__帮助您在具有相同哈希值的多个值时识别对象的情况.你可以这样做以避免这种情况…… class Test(object): def __init__(self,test = 0): self.test = test def __hash__(self): return self.test def __eq__(self,other): return self is other 在您的情况下,您只需要验证self.test值以获取对象… class Test(object): def __init__(self,other): return other.test == self.test 这就是你如何管理你的字面值! (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |