hashing mutable instances

Michael Hudson mwh at python.net
Thu Oct 30 08:47:01 EST 2003


Bernhard Herzog <bh at intevation.de> writes:

> Thomas Heller <theller at python.net> writes:
> 
> > Michael Hudson <mwh at python.net> writes:
> >
> >> Try inserting a bunch of instances of 
> >>
> >> class C:
> >>     def __hash__(self): return 0
> >>
> >> into a dictionary.
> >
> > I've though about using something like this in production code
> > to be able to store mutable instances in a dict.
> > Performance problems aside (since there are only a couple of key/value
> > pairs in the dict), is it such a bad idea?
> 
> IMO it depends on what equality means for instances. E.g. if two
> instances are only equal if they're identical, i.e. a == b is equivalent
> to a is b, then defining __hash__ can be very useful, because then you
> can use them in dictionaries and mutability doesn't really matter
> because no change to one instance can make it equal to a nother
> instance.

Um.  In that case why define __hash__ or __eq__ at all?

Cheers,
mwh

-- 
  Just put the user directories on a 486 with deadrat7.1 and turn the
  Octane into the afforementioned beer fridge and keep it in your
  office. The lusers won't notice the difference, except that you're
  more cheery during office hours.              -- Pim van Riezen, asr




More information about the Python-list mailing list