How is hash(None) calculated?

前端 未结 3 1792
半阙折子戏
半阙折子戏 2020-12-20 13:51

On my machine, hash(None) returns a value:

>>> hash(None)
-2138947203

Just out of curiosity, how is this hash value c

3条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-20 13:59

    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.

提交回复
热议问题