[Python-ideas] PEP for enum library type?

Eric Snow ericsnowcurrently at gmail.com
Wed Feb 13 02:07:31 CET 2013


On Tue, Feb 12, 2013 at 4:57 PM, Georg Brandl <g.brandl at gmx.net> wrote:
> We could even allow
>
> class Color(Enum):
>     RED = 1
>     GREEN = ...       # becomes 2
>     BLUE = ...        # 3
>     MAGENTA = 5
>     FLUFL = ...       # 6
>
> class Color(FlagEnum):
>     RED = 1
>     GREEN = ...       # becomes 2
>     BLUE = ...        # 4
>     MAGENTA = 16
>     FLUFL = ...
>
> class Color(StringEnum):
>     RED = ...         # becomes 'red'
>     GREEN = ...       # etc.
>     BLUE = ...

Nice.  Both the use of ellipsis and having the semantics depend
directly on which specific class you inherit from.

At the same time, Steven D'Aprano's proposition of "the underlying
value is irrelevant" and "must be bitwise comparable" appeals to me.
If the underlying values matter then I'd call it a "constant" rather
than an "enum".  A proper enum type would remove the temptation to use
the underlying value.

I would say that there is room for both an enum type and one or more
constants type (e.g. what Georg demonstrated above).  Regardless, this
discussion would be well-served by recognizing the distinction.

-eric



More information about the Python-ideas mailing list