Maximum/minimum Python 的 hash() 函数返回的值
Maximum/minimum value returned by Python's hash() function
上下文:构建一致的哈希算法。
Python 的 hash()
函数的 official documentation 声明:
Return the hash value of the object (if it has one). Hash values are integers.
但是,它没有明确说明函数是否映射到整数范围(具有最小值和最大值)。
来自原始类型的值有界的其他语言(例如 C#'s/Java 的 Int.MaxValue
),我知道 Python's likes to think in "unbounded" terms – 即从 [=14= 切换] 到 long
在后台。
我是否假设 hash()
函数也是无界的?或者它是否有界,例如映射到 Python 分配给 max/min values of the "int-proper" 的内容——即在 -2147483648
到 2147483647
之间?
来自文档
hash()
truncates the value returned from an object’s custom __hash__()
method to the size of a Py_ssize_t. This is typically 8 bytes on
64-bit builds and 4 bytes on 32-bit builds. If an object’s __hash__()
must interoperate on builds of different bit sizes, be sure to check
the width on all supported builds. An easy way to do this is with
python -c "import sys; print(sys.hash_info.width)
".
可以在此处找到更多详细信息https://docs.python.org/3/reference/datamodel.html#object.__hash__
正如其他人所指出的,文档中有一个错位的[1]注释:
hash() truncates the value returned from an object’s custom hash() method to the size of a Py_ssize_t.
要回答这个问题,我们需要得到这个Py_ssize_t
。经过一些研究,它 似乎 它存储在 sys.maxsize
中,尽管我希望能在这里提供一些反馈。
当时我最终采用的解决方案是:
import sys
bits = sys.hash_info.width # in my case, 64
print (sys.maxsize) # in my case, 9223372036854775807
# Therefore:
hash_maxValue = int((2**bits)/2) - 1 # 9223372036854775807, or +sys.maxsize
hash_minValue = -hash_maxValue # -9223372036854775807, or -sys.maxsize
很高兴收到关于此的 comments/feedbacks – 直到被证明是错误的,这是公认的答案。
[1] 附注 in the section dedicated to __hash__()
而不是 hash()
.
上下文:构建一致的哈希算法。
Python 的 hash()
函数的 official documentation 声明:
Return the hash value of the object (if it has one). Hash values are integers.
但是,它没有明确说明函数是否映射到整数范围(具有最小值和最大值)。
来自原始类型的值有界的其他语言(例如 C#'s/Java 的 Int.MaxValue
),我知道 Python's likes to think in "unbounded" terms – 即从 [=14= 切换] 到 long
在后台。
我是否假设 hash()
函数也是无界的?或者它是否有界,例如映射到 Python 分配给 max/min values of the "int-proper" 的内容——即在 -2147483648
到 2147483647
之间?
来自文档
hash()
truncates the value returned from an object’s custom__hash__()
method to the size of a Py_ssize_t. This is typically 8 bytes on 64-bit builds and 4 bytes on 32-bit builds. If an object’s__hash__()
must interoperate on builds of different bit sizes, be sure to check the width on all supported builds. An easy way to do this is withpython -c "import sys; print(sys.hash_info.width)
".
可以在此处找到更多详细信息https://docs.python.org/3/reference/datamodel.html#object.__hash__
正如其他人所指出的,文档中有一个错位的[1]注释:
hash() truncates the value returned from an object’s custom hash() method to the size of a Py_ssize_t.
要回答这个问题,我们需要得到这个Py_ssize_t
。经过一些研究,它 似乎 它存储在 sys.maxsize
中,尽管我希望能在这里提供一些反馈。
当时我最终采用的解决方案是:
import sys
bits = sys.hash_info.width # in my case, 64
print (sys.maxsize) # in my case, 9223372036854775807
# Therefore:
hash_maxValue = int((2**bits)/2) - 1 # 9223372036854775807, or +sys.maxsize
hash_minValue = -hash_maxValue # -9223372036854775807, or -sys.maxsize
很高兴收到关于此的 comments/feedbacks – 直到被证明是错误的,这是公认的答案。
[1] 附注 in the section dedicated to __hash__()
而不是 hash()
.