Fernando Perez fperez528 at yahoo.com
Mon Jul 19 05:31:58 CEST 2004

Chris S. wrote:

> Perhaps this is obvious to some, but why are dictionary keys constrained
> to hashable objects? why not use the object's id for the hash value.
> Wouldn't this allow typically non-hashable objects to be used as keys?
> I've done this in several instances and I've never encountered a problem.

Bad idea: objects which compare to equality may have different ids

In [1]: a=(1,2,3)

In [2]: b=(1,2,3)

In [3]: a is b
Out[3]: 0

Also, if you pickle such a dict and later restore it, all keys now hold bogus
memory addresses of non-existent stuff.

What you suggest may work in limited, highly constrained situations.  Not a good
idea for a general purpose tool like dicts.



More information about the Python-list mailing list