为什么hash()方法在Python中使用int返回简短的Hash值? [英] Why does hash() method return short Hash value with int in Python?

查看:94
本文介绍了为什么hash()方法在Python中使用int返回简短的Hash值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Python 3中调用 hash()方法时,我注意到,当使用 int 数据类型但使用 string 类型.

When hash() method is called in Python 3, I noticed that it doesn't return a long-length integer when taking in int data type but with string type.

这应该以这种方式工作吗?如果确实是这种情况,那么 int 类型的哈希值较短,由于它太短而不会引起冲突吗?

Is this supposed to work this way? If that actually is the case, for the int type to have a short hash value, won't it cause collision since it's too short?

for i in [i for i in range(5)]:
    print(hash(i))

print(hash("abc"))

结果:

0
1
2
3
4
4714025963994714141

推荐答案

在CPython中,默认的Python解释器实现内置的 hash

In CPython, default Python interpreter implementation, built-in hash is done in this way:

对于数字类型,数字x的哈希值基于减少量x以质数P = 2 ** _ PyHASH_BITS-1为模hash(x)== hash(y)只要x和y在数值上相等,即使x和y具有不同的类型

For numeric types, the hash of a number x is based on the reduction of x modulo the prime P = 2**_PyHASH_BITS - 1. It's designed so that hash(x) == hash(y) whenever x and y are numerically equal, even if x and y have different types

_PyHASH_BITS 61 (64位系统)或 31 (32位系统)(已定义

_PyHASH_BITS is 61 (64-bit systems) or 31 (32-bit systems)(defined here)

因此,在64位系统上,内置的 hash 类似于此函数:

So on 64-bit system built-in hash looks like this function:

def hash(number):
    return number % (2 ** 61 - 1)

这就是为什么对于小整数您具有相同值的原因,例如, hash(2305843009213693950)返回 2305843009213693950 hash(2305843009213693951)返回 0

That's why for small ints you got the same values, while for example hash(2305843009213693950) returns 2305843009213693950 and hash(2305843009213693951) returns 0

这篇关于为什么hash()方法在Python中使用int返回简短的Hash值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆