[Python-ideas] An identity dict

Antoine Pitrou solipsis at pitrou.net
Sun May 30 12:34:00 CEST 2010


On Sun, 30 May 2010 03:27:53 +0000 (UTC)
Benjamin Peterson <benjamin at python.org>
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().

Perhaps it would be more useful to add a generic
collections.keyfuncdict, taking a function which applied to a key
gives the real key value used for lookups.
Your identity dict would be created as:
    d = collections.keyfuncdict(id)

But of course, you can just use a normal dict:
    d = {}
    d[id(key)] = key, value

(actually, this could be how a collections.keyfuncdict gets implemented)

Regards

Antoine.





More information about the Python-ideas mailing list