什么是实现 __hash__() 的正确和好方法? [英] What's a correct and good way to implement __hash__()?

查看:33
本文介绍了什么是实现 __hash__() 的正确和好方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是实现 __hash__() 的正确和好方法?

What's a correct and good way to implement __hash__()?

我说的是返回一个哈希码的函数,然后用于将对象插入哈希表(即字典)中.

I am talking about the function that returns a hashcode that is then used to insert objects into hashtables aka dictionaries.

由于 __hash__() 返回一个整数并用于将对象装箱"到哈希表中,我假设返回的整数的值应该均匀分布于公共数据(以最小化冲突).获得这些值的好做法是什么?碰撞有问题吗?就我而言,我有一个小类,它充当容器类,其中包含一些整数、一些浮点数和一个字符串.

As __hash__() returns an integer and is used for "binning" objects into hashtables I assume that the values of the returned integer should be uniformly distributed for common data (to minimize collisions). What's a good practice to get such values? Are collisions a problem? In my case I have a small class which acts as a container class holding some ints, some floats and a string.

推荐答案

实现 __hash__() 的一种简单、正确的方法是使用键元组.它不会像专门的散列那样快,但如果你需要它,那么你可能应该在 C 中实现该类型.

An easy, correct way to implement __hash__() is to use a key tuple. It won't be as fast as a specialized hash, but if you need that then you should probably implement the type in C.

以下是使用密钥进行哈希和相等的示例:

Here's an example of using a key for hash and equality:

class A:
    def __key(self):
        return (self.attr_a, self.attr_b, self.attr_c)

    def __hash__(self):
        return hash(self.__key())

    def __eq__(self, other):
        if isinstance(other, A):
            return self.__key() == other.__key()
        return NotImplemented

此外,__hash__文档 包含更多信息,这些信息在某些特定情况下可能很有价值.

Also, the documentation of __hash__ has more information, that may be valuable in some particular circumstances.

这篇关于什么是实现 __hash__() 的正确和好方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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