如何在Python 3.2中实现__hash __?

问题描述 投票:4回答:2

我想使自定义对象可哈希化(通过腌制)。我可以找到适用于Python 2.x的__hash__算法(请参见下面的代码),但显然它来自Python 3.2的哈希值differs(我想知道为什么吗?)。有人知道__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
python algorithm hash
2个回答
5
投票

它们为何不同的答案写在这里:

哈希值现在是新值类型Py_hash_t,定义为与指针的大小相同。以前它们是长型的在某些64位操作系统上仍然只有32位长。

[散列也考虑要计算的新值,请看

 sys.hash_info 

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


3
投票

我在源代码中(unicodeobject.c)中查找了新函数,并在Python中对其进行了重新构建。这是:

def my_hash(string):
    x = ord(string[0]) << 7
    for c in string:
        x = (1000003 * x) ^ ord(c)
    x ^= len(string)
    needCorrection =  x & (1 << 65)
    x %= 2 ** 64
    if needCorrection:
        x = -~(-x ^ 0xFFFFFFFFFFFFFFFF)
    if x == -1:
        x = -2
    return x

不过,这仅是64位。现在可以纠正数字变为负数时Python的怪异行为。 (您最好不要考虑太多。)

© www.soinside.com 2019 - 2024. All rights reserved.