如何在python中实现一个好的__hash__函数 [英] How to implement a good __hash__ function in python

查看:247
本文介绍了如何在python中实现一个好的__hash__函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在实现具有多个属性的类时(如下面的玩具示例),处理散列的最佳方式是什么?

When implementing a class with multiple properties (like in the toy example below), what is the best way to handle hashing?

我猜测 __ eq __ __ hash __ 应该是一致的,但是如何实现能够处理所有属性的正确哈希函数?

I guess that the __eq__ and __hash__ should be consistent, but how to implement a proper hash function that is capable of handling all the properties?

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))

我读过这个问题元组是可散列的,所以我想知道上面的例子是否合理。是吗?

I read on this question that tuples are hashable, so I was wondering if something like the example above was sensible. Is it?

推荐答案

__ hash __ 应该返回相同的值是平等的。它也不应该改变对象的生命周期;通常你只对不可变对象实现它。

__hash__ should return the same value for objects that are equal. It also shouldn't change over the lifetime of the object; generally you only implement it for immutable objects.

一个简单的实现就是 return 0 。这总是正确的,但表现糟糕。

A trivial implementation would be to just return 0. This is always correct, but performs badly.

您的解决方案返回属性元组的哈希值是很好的。但是请注意,您不需要列出元组中所有在 __ eq __ 中比较的属性。如果某些属性通常对不平等对象具有相同的值,请将其忽略。不要让散列计算比它需要的更昂贵。

Your solution, returning the hash of a tuple of properties, is good. But note that you don't need to list all properties that you compare in __eq__ in the tuple. If some property usually has the same value for inequal objects, just leave it out. Don't make the hash computation any more expensive than it needs to be.

编辑:我建议不要使用xor混合散列。当两个不同的属性具有相同的值时,它们将具有相同的散列值,并且xor这些将相互取消。元组使用更复杂的计算来混合哈希,参见 tuplehash .crel =noreferrer> tupleobject.c

I would recommend against using xor to mix hashes in general. When two different properties have the same value, they will have the same hash, and with xor these will cancel eachother out. Tuples use a more complex calculation to mix hashes, see tuplehash in tupleobject.c.

这篇关于如何在python中实现一个好的__hash__函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆