Enumeration idioms: Values from different enumerations
eswald at gmail.com
eswald at gmail.com
Mon Dec 19 13:03:36 EST 2005
Ben Finney wrote:
> Antoon Pardon wrote:
> > I just downloaded your enum module for python [from the Cheeseshop]
> > and played a bit with it. IMO some of the behaviour makes it less
> > usefull.
> [...]
> > I also think it would be more usefull if enums from different
> > enumerations just tested unequal.
> [...]
>
> However, I'm aware that this is not how other Python types behave:
>
> >>> 23 == 23
> True
> >>> 42 == 23
> False
> >>> "spam" == 23
> False
>
> Is there a semantic difference between these cases? Is that difference
> enough to want different behaviour?
>
> Is there some behaviour other than "evaluate to False" or "raise an
> exception", that could indicate "not comparable"?
Yes: return NotImplemented. Note that the == operator automagically
returns False in this case.
>>> "spam".__eq__("spam")
True
>>> "spam".__eq__("ham")
False
>>> "spam".__eq__(23)
NotImplemented
This way, the user could explicitly call __eq__ and check for
NotImplemented if he desires the exceptional behavior.
- Eric
More information about the Python-list
mailing list