dictionary keys, __hash__, __cmp__
Michael Hudson
mwh at python.net
Wed Nov 5 09:13:31 EST 2003
Jan-Erik Meyer-Lütgens <python at meyer-luetgens.de> writes:
> Michael Hudson wrote:
> > Jan-Erik Meyer-Lütgens <python at meyer-luetgens.de> writes:
> >>
> >> 3. "If a class does not define a __cmp__() method it
> >> should not define a __hash__() operation either."
> >>
> >>
> >>Can I asume that:
> >> -- I can savely ignore the 3rd statement, because python
> >> falls back to cmp(id(obj1), id(obj2)), if __cmp__()
> >> is not defined.
> > Don't understand.
> >
>
> Ok, let me ask the following question: What is the reason
> for that rule?
Well, the idea is that dictionary lookup is done by equality. If you
don't define __cmp__ then equality is in fact identity (well, that's
very nearly true...) so the default implementation of __hash__ (based
on the pointer address) is as good as it can get (you have hash
equality iff you have object equality).
I think.
Cheers,
mwh
--
Every now and then, Google doesn't throw up what I need so I start
checking Altavista, Yahoo, etc. In almost every single case, I am
brutally reminded why I use Google in the first place.
-- John Riddoch, asr
More information about the Python-list
mailing list