Tuple question
greg
greg at cosc.canterbury.ac.nz
Sun Sep 5 22:00:58 EDT 2004
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.
> > 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.
Greg
More information about the Python-list
mailing list