散列(无)如何计算? [英] How is hash(None) calculated?
问题描述
在我的机器上, hash(None)
返回一个值:
>>>散列(无)
-2138947203
出于好奇,这个散列值是如何计算的?它看起来好像不是基于 None
的 id
,因为如果我重新启动Python解释器。
id code>,但None是被定义为C全局变量的少数Python对象之一,所以它的地址(通常)在Python运行之间不会改变。其他这样的对象是 True
和 False
(但是它们被散列为整数),或者内置类如 object
和元组
。
地址(和散列)是但是,不同的CPython版本不同。在我的系统中, hash(无)
给出539708。
On my machine, hash(None)
returns a value:
>>> hash(None)
-2138947203
Just out of curiosity, how is this hash value calculated? It doesn't seem as though this value is based on None
's id
as it is the same if I restart the Python interpreter.
解决方案 It is based on None's id
, but None is one of a few Python objects that are defined as C global variables, so its address (typically) doesn't change between Python runs. Other such objects are True
and False
(but these are hashed as ints), or built-in classes like object
and tuple
.
The address (and hash) is different between different CPython builds, however. On my system, hash(None)
gives 539708.
这篇关于散列(无)如何计算?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!