Python hash() 无法处理长整数?

标签 python hash integer

我定义了一个类:

class A:
    ''' hash test class
    >>> a = A(9, 1196833379, 1, 1773396906)
    >>> hash(a)
    -340004569

    This is weird, 12544897317L expected.
    '''
    def __init__(self, a, b, c, d):
        self.a = a
        self.b = b
        self.c = c
        self.d = d

    def __hash__(self):
        return self.a * self.b + self.c * self.d

为什么在 doctest 中,hash() 函数给出一个负整数?

最佳答案

它似乎仅限于 32 位。通过阅读this question ,看起来您的代码可能已经在 64 位机器上产生了预期的结果(具有这些特定值,因为结果适合 64 位)。

内置 hash 函数的结果依赖于平台并受限于本地字的大小。如果您需要确定性的跨平台哈希,请考虑使用 hashlib 模块。

关于Python hash() 无法处理长整数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2687829/

相关文章:

python - 带和不带放回的加权随机选择

hash - 如何减少哈希值的长度?

c++ - 如何验证整数输入?

python - 如何让 SVM 很好地处理 scikit-learn 中的缺失数据?

python - 如何在 python 中使用带有 datetime 对象的时区?

python - 在 os.walk() 期间找到绝对路径的更好方法?

java - 什么是散列中的折叠技术以及如何实现它?

java - 在 Android 4.2 及更早版本中使用 PBKDF2WithHmacSHA1 和 DESede/CBC/PKCS5Padding

c - C中的8位到4位整数转换,将signed int转换为-8?

Vb.net 将整数 DBNULL 转换为 0 - 错误