python的整数哈希
我知道,一个不变的对象的哈希是该对象的整数表示,这是该对象在过程的寿命中是独一无二的。
整数对象的哈希与整数持有的值相同。例如,
>>> int(1000).__hash__()
1000
但是当整数成长足够大时,原则上的一定阈值似乎是在原则上突破的。它的价值似乎在一定的范围内。
>>> int(10000000000000000).__hash__()
10000000000000000
>>> int(100000000000000000).__hash__()
100000000000000000
>>> int(1000000000000000000).__hash__()
1000000000000000000
>>> int(10000000000000000000).__hash__()
776627963145224196
两个问题:
- 限制是什么?哈希桌子覆盖的整数空间是什么?
- 整数如何计算的哈希值超过上述限制?
系统信息:
Linux lap-0179 5.13.0-44-generic #49~20.04.1-Ubuntu SMP Wed May 18 18:44:28 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux
Python解释器:
Python 3.8.10 (default, Mar 15 2022, 12:22:08)
[GCC 9.4.0] on linux
I understand that hash of an immutable object is an integer representation of that object which is unique within the process's lifetime.
Hash of an integer object is the same as the value held by the integer. For example,
>>> int(1000).__hash__()
1000
But when the integer grows big enough, above principle breaks after a certain threshold as it seems. Its value seems to be rolled with in some limit.
>>> int(10000000000000000).__hash__()
10000000000000000
>>> int(100000000000000000).__hash__()
100000000000000000
>>> int(1000000000000000000).__hash__()
1000000000000000000
>>> int(10000000000000000000).__hash__()
776627963145224196
Two questions:
- What is the limit? What is the integer-space that hash table covers?
- How is the hash value calculated for an integer exceeding the above limit?
System information:
Linux lap-0179 5.13.0-44-generic #49~20.04.1-Ubuntu SMP Wed May 18 18:44:28 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux
Python interpreter:
Python 3.8.10 (default, Mar 15 2022, 12:22:08)
[GCC 9.4.0] on linux
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
虽然这是机器和实现依赖的,但对于64位计算机上的Cpython,对于非负整数
n
的hash()
被计算为n%k
带有k =(2 ** 61-1)
(= 2305843009213693951
)因此,0
k-- 1 留下来。这是从经验上证明的:
有关完整的规则集,请参见文档。
While this is machine and implementation dependent, for CPython on 64-bit machines, the
hash()
for a non-negative integern
is computed asn % k
withk = (2 ** 61 - 1)
(= 2305843009213693951
) hence values between0
andk - 1
are left as is.This is empirically evidenced here:
For the complete ruleset, see the documentation.