[Python-ideas] An identity dict

Imri Goldberg lorgandon at gmail.com
Mon May 31 00:34:59 CEST 2010


On Sun, May 30, 2010 at 12:07 PM, Lie Ryan <lie.1296 at gmail.com> wrote:

> On 05/30/10 13:27, Benjamin Peterson wrote:
> > In the spirit of collections.OrderedDict and collections.defaultdict, I'd
> like
> > to propose collections.identitydict. It would function just like a normal
> > dictionary, but ignore hash values and comparison operators and merely
> lookup
> > keys based on the key's id().
> >
> > This dict is very useful for keep track of objects that should not be
> compared
> > by normal comparison methods. For example, I would use an identitydict to
> hold
> > weak references that would otherwise fallback to their referant object's
> hash.
>

My 2 bits:

1. I implemented and used such a dict a few years ago, as part of a
graph/tree algorithm. While I don't have access to the source anymore, I
mainly wanted to bring up an example for the usage of such a data structure.
Still, I haven't needed it since, so take from it what you will.


> what's wrong with dict[id(key)] = foo?
>

2. Mostly that you want other operators to work as well. In your example,
"key in dic" will return False, as __contains__ is the standard
implementation.

Cheers,
Imri

-- 
Imri Goldberg
--------------------------------------
http://plnnr.com/ - automatic trip planning
http://www.algorithm.co.il/blogs/
--------------------------------------
-- insert signature here ----
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20100531/7e4a5add/attachment.html>


More information about the Python-ideas mailing list