python - __hash__ 在 Python 3.2 中是如何实现的?

标签 python algorithm hash

我想使自定义对象可哈希化(通过酸洗)。我可以找到适用于 Python 2.x 的 __hash__ 算法(请参阅下面的代码),但它显然不同于适用于 Python 3.2 的哈希(我想知道为什么?)。有人知道 __hash__ 在 Python 3.2 中是如何实现的吗?

#Version: Python 3.2

def c_mul(a, b):
    #C type multiplication
    return eval(hex((int(a) * b) & 0xFFFFFFFF)[:-1])

class hs:
    #Python 2.x algorithm for hash from http://effbot.org/zone/python-hash.htm
    def __hash__(self):
        if not self:
            return 0 # empty
        value = ord(self[0]) << 7
        for char in self:
            value = c_mul(1000003, value) ^ ord(char)
        value = value ^ len(self)
        if value == -1:
            value = -2
        return value


def main():
    s = ["PROBLEM", "PROBLEN", "PROBLEO", "PROBLEP"]#, "PROBLEQ", "PROBLER", "PROBLES"]
    print("Python 3.2 hash() bild-in")
    for c in s[:]: print("hash('", c, "')=", hex(hash(c)),  end="\n")
    print("\n")
    print("Python 2.x type hash: __hash__()")
    for c in s[:]: print("hs.__hash__('", c, "')=", hex(hs.__hash__(c)),  end="\n")


if __name__ == "__main__":
    main()

OUTPUT:
Python 3.2 hash() bild-in
hash(' PROBLEM ')= 0x7a8e675a
hash(' PROBLEN ')= 0x7a8e6759
hash(' PROBLEO ')= 0x7a8e6758
hash(' PROBLEP ')= 0x7a8e6747


Python 2.x type hash: __hash__()
hs.__hash__(' PROBLEM ')= 0xa638a41
hs.__hash__(' PROBLEN ')= 0xa638a42
hs.__hash__(' PROBLEO ')= 0xa638a43
hs.__hash__(' PROBLEP ')= 0xa638a5c

最佳答案

为什么他们不同的答案写在那里:

Hash values are now values of a new type, Py_hash_t, which is defined to be the same size as a pointer. Previously they were of type long, which on some 64-bit operating systems is still only 32 bits long.

哈希也考虑了要计算的新值,看看

 sys.hash_info 

对于字符串,可以看看http://svn.python.org/view/python/trunk/Objects/stringobject.c?view=markup第 1263 行 string_hash(PyStringObject *a)

关于python - __hash__ 在 Python 3.2 中是如何实现的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6008026/

相关文章:

javascript - html中python代码和javascript代码的执行顺序是什么

python - 为什么小数不能与 float 互操作

python - 检查字符串的 SHA1

algorithm - 计算数据哈希的安全算法

python 3 : hook list & dict changes

python - 如何制作一个接受 POST 数据的 Django-Rest-Framework API?

c# - 监控变化的应用程序

javascript - 将相邻的行组合成一个数组

algorithm - 字符串的哈希算法

c - 如何在 C 中哈希 CPU ID