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's based on the address of
None
in memory, as the type definition says.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 areTrue
andFalse
(but these are hashed as ints), or built-in classes likeobject
andtuple
.The address (and hash) is different between different CPython builds, however. On my system,
hash(None)
gives 539708.As
None
is an object, I've wrote a functionobject_hash
for calculation of object hash:The resulting hashes are equal: