PEP 354: Enumerations in Python

Giovanni Bajo noway at sorry.com
Mon Feb 27 01:09:35 EST 2006


Kay Schluehr wrote:

>> The enumerations specified in this PEP are instances of an ``enum``
>> type.  Some alternative designs implement each enumeration as its own
>> class, and a metaclass to define common properties of all
>> enumerations.
>>
>> One motivation for having a class (rather than an instance) for each
>> enumeration is to allow subclasses of enumerations, extending and
>> altering an existing enumeration.  A class, though, implies that
>> instances of that class will be created; it is difficult to imagine
>> what it means to have separate instances of a "days of the week"
>> class, where each instance contains all days.  This usually leads to
>> having each class follow the Singleton pattern, further complicating
>> the design.
>>
>> In contrast, this PEP specifies enumerations that are not expected to
>> be extended or modified.  It is, of course, possible to create a new
>> enumeration from the string values of an existing one, or even
>> subclass the ``enum`` type if desired.
>
> Maybe a metaclass implementation complicates design, but usage is
> quite simple and flexible. The classics is already referenced by the
> Python docs:
>
> http://www.python.org/doc/essays/metaclasses/Enum.py

Agreed. Allowing subclassing of an existing enum is a strong plus to me.
-- 
Giovanni Bajo





More information about the Python-list mailing list