Enumeration idioms: Values from different enumerations

Ben Finney bignose+hates-spam at benfinney.id.au
Mon Dec 19 23:16:03 CET 2005


eswald at gmail.com writes:
> Ben Finney wrote:
>> 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.

Thanks, I was unaware of the standard usage of NotImplemented. That
seems like a sensible solution.

    <URL:http://docs.python.org/ref/types.html#l2h-29>

-- 
 \        "My doctor told me to stop having intimate dinners for four. |
  `\            Unless there are three other people."  -- Orson Welles |
_o__)                                                                  |
Ben Finney



More information about the Python-list mailing list