[Python-ideas] PEP for enum library type?

Stephen J. Turnbull stephen at xemacs.org
Wed Feb 13 04:41:20 CET 2013


Stefan Krah writes:
 > Guido van Rossum <guido at python.org> wrote:
 > > Please. Anything *except*
 > > 
 > > class Color(Enum):
 > >   RED, BLUE, GREEN

I'll take you at your word.  But maybe you should stop reading
now.<wink/>

 > Is a new enum keyword still a possibility? To me that seems to be the
 > cleanest way.

If parser support were on the table, couldn't we avoid a new keyword
by aping the Pascal syntax with

    class Color(Enum) = [RED, GREEN, BLUE]

or something like that?  I realize this is horrible because of the
missing colon, and because availability of the syntax depends on the
base class.  Maybe it will suggest something to somebody, though.  The
base class seems to be needed to support variants like OrderedEnum and
FlagEnum, as the implicit Enum

    class Color = [RED, GREEN, BLUE]

can't disambiguate common use cases (ordered enums, or-able flags).
Another possibility would be to allow the list of identifiers as the
base class:

    class Color([RED, GREEN, BLUE]):
        pass

I suppose this is better than the version using "=", which doesn't
seem to allow explicit initialization of the enumerated ids, or
operations on them, in the class body.  Perhaps the Enum base class
should be explicit:

    class Color(Enum, [RED, GREEN, BLUE]):
        pass

In all cases the point of using lists instead of (implicit) tuples is
to clearly guarantee order of evaluation to support OrderedEnum.  Sets
are out because FlagEnum doesn't need such support.




More information about the Python-ideas mailing list