Enumeration idioms: Values from different enumerations

skip at pobox.com skip at pobox.com
Fri Dec 16 16:37:24 CET 2005

    >>>> ShirtSize = Enum('small', 'medium', 'large')
    >>>> AppleSize = Enum('small', 'large')

    Ben> What should be the result of this comparison::

    >>>> ShirtSize.small == AppleSize.small

False.  They are values from different objects.  Just make __eq__ map to
"is".  I think you'll be fine.

    Ben> Or is it an error to even try comparing them?

As someone else pointed out containment tests become difficult with your
current formulation.

    >> Do any python builtins behave that way? How about anything in the
    >> python standard library?

    Ben> No to both; I believe this may be a defining property of
    Ben> enumerations. Am I wrong?

I think so. <0.5 wink>.  I think you should be able to compare any two
objects.  I think the complex types give you a little wiggle room on the
size comparisons (<, <=, >, >=), but I think == and != really ought to work.


More information about the Python-list mailing list