[Python-Dev] collections.idset and collections.iddict? (original) (raw)

Jack Diederich jack at performancedrivers.com
Tue Mar 7 01:21:26 CET 2006


On Mon, Mar 06, 2006, 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 assumed Neil wanted a container that was id() sensitive, I've done this occasionally myself to see if an object is in a container and not just an object equivalent to the one I am checking for.

a = set([1,2,3,4]) b = set([1,2,3,4]) a == b True a is b False container = [a] b in container True container = [id(a)] id(b) in container False

-Jack



More information about the Python-Dev mailing list