Hash of None varies per-machine

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Sat Apr 4 13:44:52 CEST 2009

On Sat, 04 Apr 2009 13:09:06 +0200, Hendrik van Rooyen wrote:

>>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?

You 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? 

It just works.

> - 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.

Seems to me you have misunderstood the way pickling works.

On one machine:

>>> import pickle
>>> pickle.dump({None: "hello world"}, open("pickled", 'w'))

And then on another:

>>> import pickle
>>> pickle.load(open('pickled'))
{None: 'hello world'}

It just works.


More information about the Python-list mailing list