Tuple question
Alex Martelli
aleaxit at yahoo.com
Sun Sep 5 16:53:56 EDT 2004
Roy Smith <roy at panix.com> wrote:
> I asked:
> > How is a class instance's mutability any less of disqualifier for
> > key-ness than a list's mutability?
>
> Benjamin Niemann <pink at odahoda.de> wrote:
>
> > a = [1, 2, 3]
> > b = [1, 2, 3]
> > if a == b:
> > print "List equality is based on content"
>
> 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?
> 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? Which part of a list's behavior makes it inherently
> unusable as a key? I'm not asking about design philosophy, I'm asking
> about observable behavior.
This was discussed in detail in another thread about 2 days ago, I
believe. That thread started by somebody asking why modules were
hashable (could be keys in a dictionary).
Alex
More information about the Python-list
mailing list