gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 19:34:58 26 4
gpt4 key购买 nike

我定义了一个类:

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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com