[Python-Dev] collections.idset and collections.iddict?

Bob Ippolito bob at redivi.com
Tue Mar 7 01:43:01 CET 2006


On Mar 6, 2006, at 4:14 PM, Guido van Rossum wrote:

> On 3/6/06, Raymond Hettinger <python at rcn.com> wrote:
>> [Neil Schemenauer]
>>> I occasionally need dictionaries or sets that use object identity
>>> rather than __hash__ to store items.  Would it be appropriate to add
>>> these to the collections module?
>>
>> Why not decorate the objects with a class adding a method:
>>    def __hash__(self):
>>        return id(self)
>>
>> That would seem to be more Pythonic than creating custom variants  
>> of other
>> containers.
>
> I hate to second-guess the OP, but you'd have to override __eq__ too,
> and probably __ne__ and __cmp__ just to be sure. And probably that
> wouldn't do -- since the default __hash__ and __eq__ have the desired
> behavior, the OP is apparently talking about objects that override
> these operations to do something meaningful; overriding them back
> presumably breaks other functionality.
>
> I wonder if this use case and the frequently requested
> case-insensitive dict don't have some kind of generalization in common
> -- perhaps a dict that takes a key function a la list.sort()?

+1.  I've wanted such a thing a couple times, and there is some  
precedent in the stdlib (e.g. WeakKeyDictionary would be a lot  
shorter with such a base class).

-bob



More information about the Python-Dev mailing list