PEP 354: Enumerations in Python

Giovanni Bajo noway at sorry.com
Mon Feb 27 01:15:12 EST 2006


Ben Finney wrote:

> Values within an enumeration cannot be meaningfully compared except
> with values from the same enumeration.  The comparison operation
> functions return ``NotImplemented`` [#CMP-NOTIMPLEMENTED]_ when a
> value from an enumeration is compared against any value not from the
> same enumeration or of a different type::
> [...]
> This allows the operation to succeed, evaluating to a boolean value::

Given that this is going to change in Python 3.0 for builtin types, I'm not
sure there's much reason to keep it this way. I'd rather a comparison operation
between different enum types to raise an exception. This would be a very
compelling feature to use enum (and in fact, the implementation I chose in
Cookbook for my programs have this feature).

> Coercing a value from an enumeration to a ``str`` results in the
> string that was specified for that value when constructing the
> enumeration::
>
>     >>> gym_night = Weekdays.wed
>     >>> str(gym_night)
>     'wed'

What's the repr of an enumeration value? OTOH, it should be something like
"Weekdays.wed", so that eval(repr()) holds true. Also, it'd be very useful in
debug dumps, tracebacks and whatnot.
-- 
Giovanni Bajo





More information about the Python-list mailing list