aleaxit at yahoo.com
Mon Sep 6 09:34:03 CEST 2004
greg <greg at cosc.canterbury.ac.nz> wrote:
> Alex Martelli wrote:
> > Roy Smith <roy at panix.com> wrote:
> > > Tuple (and string) equality is based on content too. So what? I can
> > > give my data class an __eq__ method, and then my class instance equality
> > > would also based on content.
> > And your class's instances wouldn't then be hashable any more unless
> > they defined a __hash__ method -- have you tried?
> And even if you did give it a __hash__ method, you wouldn't
> be able to get it to work properly as a dict key. It's an
> inescapable fact of the way dicts work.
Well, it depends on how you define 'properly'. Semantics CAN be
respected, it's _performance_ that may disappoint;-).
> > > So, to restate my original question, why should my mutable,
> > > content-based-eqality class instance be a valid dictionary key,
> > > when a list is not?
> It wouldn't be a valid dictionary key. You might be able to fool
> Python into accepting it, but it would malfunction.
It might be made to function, just VERY slowly:
def __hash__(self): return 42
Containing this will make ANY class hashable no matter what. But you'd
better buy a new and very fast machine if you're gonna use multiple
instances of such a class to key into a dictionary...;-)
More information about the Python-list