Identity dictionnary

Jean-Sebastien Roy js at jeannot.org
Sun Feb 29 03:49:54 CET 2004


In article <2004022821314075249%bob at redivicom>,
 Bob Ippolito <bob at redivi.com> wrote:

> > Is defining __hash__ as id(self) sufficient to guarantee that __eq__ 
> > will never be called by the dict implementation ? (it seems to work in 
> > practice)
> > If not, how to ensure __eq__ will never be called ?
> 
> I don't believe your assumption about dict is correct.  A typical dict 
> is not going to have 2**32 slots in it, so you will eventually get a 
> collision, which will call __eq__ unless the key you're getting happens 
> to be the first item in that slot (it think uses object identity first 
> as an optimization and for sanity, though I have not looked at the 
> source).

A few years ago, Tim Peters said that == what not called unless __hash__ 
are equals (whatever small the number of slots is (quite strange indeed, 
but why not ?)).

But that may not be true anymore (that's why I asked).

> What you might want is a special kind of container, not a special kind 
> of object to use as keys to a dict:
> 
> class identitymap(object):
>     def __init__(self):
>         self._dict = {}
> 
>     def __getitem__(self, item):
>         return self._dict[id(item)][1]
> 
>     def __setitem__(self, item, value):
>         self._dict[id(item)] = (item, value)
> 
> ... etc.
> 
> I have used a data structure such as this before for similar reasons 
> (an attempt at pure python transparent object persistence..).

I initialy ruled out reimplementing a special dictionnary because I'm a 
bit worried about the performance, but that's a nice solution : thanks ! 
I'll probably go with it.

Regards,

js



More information about the Python-list mailing list