首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在python中实现一个good_hash_function?

如何在python中实现一个good_hash_function?

提问于 2018-03-02 00:22:28
回答 2关注 0查看 91

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

我猜__eq____hash__应该是一致的,但是如何实现一个能够处理所有属性的正确的散列函数呢?

代码语言:txt
复制
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))

所以我想知道上面的例子是否合理。是吗?

回答

和开发者交流更多问题细节吧,去 写回答
相关文章

相似问题

相关问答用户
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档