Hash of None varies per-machine

Hendrik van Rooyen mail at microcorp.co.za
Sat Apr 4 13:09:06 CEST 2009

"Steven D'Aprano" <steve at REMOVE..urce.com.au> wrote:

On Fri, 03 Apr 2009 10:50:08 -0700, ben.taylor wrote:

>> 2. Should the hash of None vary per-machine? I can't think why you'd
>> write code that would rely on the value of the hash of None, but you
>> might I guess.
>The value of hash(None) appears to be the value of id(None), which means 
>it is the memory address that None happens to get, which means it will 
>depend on the precise order that Python allocates things when it starts 
>up, which will vary from platform to platform and version to version.
>> 3. Given that presumably not all things can be hashed (since the
>> documentation description of hash() says it gives you the hash of the
>> object "if it can be hashed"), should None be hashable?
>Any object can be hashed if it has a working __hash__ method. There's no 
>reason not to have None hashable -- it costs nothing and allows you to 
>use None as a dict key.

So what happens if I try to pickle the dict and keep it for next time?
Will I be able to access whatever I have associated with None?
(directly -  mydict[None], not in a for loop.)
And if I send the pickle to another machine and unpickle it,
what then? - is unpickling smart enough to construct the dict
with the local hash of None?

- Seems to me that if it isn't, and you want to do this, there would 
   have to be a fixed, well known value for the hash of None.

- Hendrik

More information about the Python-list mailing list