2013-02-23 09:36:35 -05:00
|
|
|
PEP: 435
|
|
|
|
Title: Adding an Enum type to the Python standard library
|
|
|
|
Version: $Revision$
|
|
|
|
Last-Modified: $Date$
|
|
|
|
Author: Barry Warsaw <barry@python.org>,
|
|
|
|
Eli Bendersky <eliben@gmail.com>
|
|
|
|
Status: Draft
|
|
|
|
Type: Standards Track
|
|
|
|
Content-Type: text/x-rst
|
|
|
|
Created: 2013-02-23
|
|
|
|
Python-Version: 3.4
|
|
|
|
Post-History: 2013-02-23
|
|
|
|
|
|
|
|
|
|
|
|
Abstract
|
|
|
|
========
|
|
|
|
|
|
|
|
This PEP proposes adding an enumeration type to the Python standard library.
|
2013-02-25 10:15:03 -05:00
|
|
|
Specifically, it proposes moving the existing ``flufl.enum`` package by Barry
|
|
|
|
Warsaw into the standard library. Much of this PEP is based on the "using"
|
|
|
|
[1]_ document from the documentation of ``flufl.enum``.
|
2013-02-23 09:36:35 -05:00
|
|
|
|
2013-04-04 09:52:14 -04:00
|
|
|
An enumeration is a set of symbolic names bound to unique, constant values.
|
|
|
|
Within an enumeration, the values can be compared by identity, and the
|
|
|
|
enumeration itself can be iterated over.
|
2013-02-23 09:36:35 -05:00
|
|
|
|
|
|
|
|
2013-04-02 10:09:55 -04:00
|
|
|
Decision
|
|
|
|
========
|
|
|
|
|
|
|
|
TODO: update decision here once pronouncement is made.
|
|
|
|
|
|
|
|
|
2013-02-23 09:36:35 -05:00
|
|
|
Status of discussions
|
|
|
|
=====================
|
|
|
|
|
2013-02-25 10:15:03 -05:00
|
|
|
The idea of adding an enum type to Python is not new - PEP 354 [2]_ is a
|
|
|
|
previous attempt that was rejected in 2005. Recently a new set of discussions
|
|
|
|
was initiated [3]_ on the ``python-ideas`` mailing list. Many new ideas were
|
|
|
|
proposed in several threads; after a lengthy discussion Guido proposed adding
|
2013-03-15 10:25:45 -04:00
|
|
|
``flufl.enum`` to the standard library [4]_. During the PyCon 2013 language
|
|
|
|
summit the issue was discussed further. It became clear that many developers
|
|
|
|
want to see an enum that subclasses ``int``, which can allow us to replace
|
|
|
|
many integer constants in the standard library by enums with friendly string
|
|
|
|
representations, without ceding backwards compatibility. An additional
|
|
|
|
discussion among several interested core developers led to the proposal of
|
|
|
|
having ``IntEnum`` as a special case of ``Enum``.
|
|
|
|
|
|
|
|
The key dividing issue between ``Enum`` and ``IntEnum`` is whether comparing
|
|
|
|
to integers is semantically meaningful. For most uses of enumerations, it's
|
|
|
|
a **feature** to reject comparison to integers; enums that compare to integers
|
|
|
|
lead, through transitivity, to comparisons between enums of unrelated types,
|
|
|
|
which isn't desirable in most cases. For some uses, however, greater
|
|
|
|
interoperatiliby with integers is desired. For instance, this is the case for
|
|
|
|
replacing existing standard library constants (such as ``socket.AF_INET``)
|
|
|
|
with enumerations.
|
|
|
|
|
|
|
|
This PEP is an attempt to formalize this decision as well as discuss a number
|
2013-04-12 09:22:43 -04:00
|
|
|
of variations that were proposed and can be considered for inclusion.
|
2013-02-25 10:15:03 -05:00
|
|
|
|
2013-02-23 09:36:35 -05:00
|
|
|
|
|
|
|
Motivation
|
|
|
|
==========
|
|
|
|
|
|
|
|
*[Based partly on the Motivation stated in PEP 354]*
|
|
|
|
|
2013-02-25 10:15:03 -05:00
|
|
|
The properties of an enumeration are useful for defining an immutable, related
|
|
|
|
set of constant values that have a defined sequence but no inherent semantic
|
|
|
|
meaning. Classic examples are days of the week (Sunday through Saturday) and
|
|
|
|
school assessment grades ('A' through 'D', and 'F'). Other examples include
|
|
|
|
error status values and states within a defined process.
|
2013-02-23 09:36:35 -05:00
|
|
|
|
2013-02-25 10:15:03 -05:00
|
|
|
It is possible to simply define a sequence of values of some other basic type,
|
|
|
|
such as ``int`` or ``str``, to represent discrete arbitrary values. However,
|
|
|
|
an enumeration ensures that such values are distinct from any others including,
|
|
|
|
importantly, values within other enumerations, and that operations without
|
|
|
|
meaning ("Wednesday times two") are not defined for these values. It also
|
|
|
|
provides a convenient printable representation of enum values without requiring
|
|
|
|
tedious repetition while defining them (i.e. no ``GREEN = 'green'``).
|
2013-02-23 09:36:35 -05:00
|
|
|
|
|
|
|
|
2013-02-25 10:15:03 -05:00
|
|
|
Module and type name
|
|
|
|
====================
|
2013-02-23 09:36:35 -05:00
|
|
|
|
|
|
|
We propose to add a module named ``enum`` to the standard library. The main
|
2013-02-23 12:16:16 -05:00
|
|
|
type exposed by this module is ``Enum``. Hence, to import the ``Enum`` type
|
|
|
|
user code will run::
|
2013-02-23 09:36:35 -05:00
|
|
|
|
2013-02-23 12:16:16 -05:00
|
|
|
>>> from enum import Enum
|
2013-02-23 09:36:35 -05:00
|
|
|
|
2013-02-25 10:15:03 -05:00
|
|
|
|
2013-02-23 09:36:35 -05:00
|
|
|
Proposed semantics for the new enumeration type
|
|
|
|
===============================================
|
|
|
|
|
|
|
|
Creating an Enum
|
|
|
|
----------------
|
|
|
|
|
|
|
|
Enumerations are created using the class syntax, which makes them easy to read
|
2013-04-06 00:02:21 -04:00
|
|
|
and write. An alternative creation method is described in `Convenience API`_.
|
2013-02-25 10:15:03 -05:00
|
|
|
To define an enumeration, derive from the ``Enum`` class and add attributes
|
|
|
|
with assignment to their integer values::
|
2013-02-23 09:36:35 -05:00
|
|
|
|
|
|
|
>>> from enum import Enum
|
|
|
|
>>> class Colors(Enum):
|
|
|
|
... red = 1
|
|
|
|
... green = 2
|
|
|
|
... blue = 3
|
|
|
|
|
2013-02-23 12:16:16 -05:00
|
|
|
Enumeration values have nice, human readable string representations::
|
2013-02-23 09:36:35 -05:00
|
|
|
|
|
|
|
>>> print(Colors.red)
|
|
|
|
Colors.red
|
|
|
|
|
2013-02-23 12:16:16 -05:00
|
|
|
...while their repr has more information::
|
2013-02-23 09:36:35 -05:00
|
|
|
|
|
|
|
>>> print(repr(Colors.red))
|
2013-04-05 13:52:37 -04:00
|
|
|
<EnumValue: Colors.red [value=1]>
|
2013-02-23 09:36:35 -05:00
|
|
|
|
2013-02-23 12:16:16 -05:00
|
|
|
The enumeration value names are available through the class members::
|
2013-02-23 09:36:35 -05:00
|
|
|
|
|
|
|
>>> for member in Colors.__members__:
|
|
|
|
... print(member)
|
|
|
|
red
|
|
|
|
green
|
|
|
|
blue
|
|
|
|
|
2013-04-12 15:01:05 -04:00
|
|
|
You can get the enumeration class object from an enumeration value::
|
2013-02-23 09:36:35 -05:00
|
|
|
|
|
|
|
>>> cls = Colors.red.enum
|
|
|
|
>>> print(cls.__name__)
|
|
|
|
Colors
|
|
|
|
|
2013-02-23 12:16:16 -05:00
|
|
|
Enums also have a property that contains just their item name::
|
2013-02-23 09:36:35 -05:00
|
|
|
|
|
|
|
>>> print(Colors.red.name)
|
|
|
|
red
|
|
|
|
>>> print(Colors.green.name)
|
|
|
|
green
|
|
|
|
>>> print(Colors.blue.name)
|
|
|
|
blue
|
|
|
|
|
2013-02-23 12:16:16 -05:00
|
|
|
The str and repr of the enumeration class also provides useful information::
|
2013-02-23 09:36:35 -05:00
|
|
|
|
|
|
|
>>> print(Colors)
|
|
|
|
<Colors {red: 1, green: 2, blue: 3}>
|
|
|
|
>>> print(repr(Colors))
|
|
|
|
<Colors {red: 1, green: 2, blue: 3}>
|
|
|
|
|
2013-04-12 10:53:42 -04:00
|
|
|
The ``Enum`` class supports iteration. Iteration order is undefined::
|
2013-02-23 09:36:35 -05:00
|
|
|
|
2013-04-12 10:53:42 -04:00
|
|
|
>>> from operator import attrgetter
|
|
|
|
>>> by_value = attrgetter('value')
|
2013-04-05 13:52:37 -04:00
|
|
|
>>> class FiveColors(Enum):
|
|
|
|
... pink = 4
|
|
|
|
... cyan = 5
|
|
|
|
... green = 2
|
|
|
|
... blue = 3
|
|
|
|
... red = 1
|
2013-04-12 10:53:42 -04:00
|
|
|
>>> [v.name for v in sorted(FiveColors, by_value)]
|
2013-04-04 09:52:14 -04:00
|
|
|
['red', 'green', 'blue', 'pink', 'cyan']
|
2013-02-23 09:36:35 -05:00
|
|
|
|
2013-04-12 10:53:42 -04:00
|
|
|
Iteration order over ``IntEnum`` enumerations are guaranteed to be
|
|
|
|
sorted by value.
|
|
|
|
|
|
|
|
>>> class Toppings(IntEnum):
|
|
|
|
... anchovies = 4
|
|
|
|
... black_olives = 8
|
|
|
|
... cheese = 2
|
|
|
|
... dried_tomatoes = 16
|
|
|
|
... eggplant = 1
|
|
|
|
|
|
|
|
>>> for value in Toppings:
|
|
|
|
... print(value.name, '=', value.value)
|
|
|
|
eggplant = 1
|
|
|
|
cheese = 2
|
|
|
|
anchovies = 4
|
|
|
|
black_olives = 8
|
|
|
|
dried_tomatoes = 16
|
|
|
|
|
2013-04-04 09:52:14 -04:00
|
|
|
Enumeration values are hashable, so they can be used in dictionaries and sets::
|
2013-02-23 09:36:35 -05:00
|
|
|
|
2013-04-04 09:52:14 -04:00
|
|
|
>>> apples = {}
|
|
|
|
>>> apples[Colors.red] = 'red delicious'
|
|
|
|
>>> apples[Colors.green] = 'granny smith'
|
|
|
|
>>> apples
|
|
|
|
{<EnumValue: Colors.green [value=2]>: 'granny smith', <EnumValue: Colors.red [value=1]>: 'red delicious'}
|
|
|
|
|
2013-04-10 00:18:55 -04:00
|
|
|
To programmatically access enumeration values, use ``getattr``::
|
2013-04-04 09:52:14 -04:00
|
|
|
|
2013-04-10 00:18:55 -04:00
|
|
|
>>> getattr(Colors, 'red')
|
2013-04-05 13:52:37 -04:00
|
|
|
<EnumValue: Colors.red [value=1]>
|
2013-04-04 09:52:14 -04:00
|
|
|
|
|
|
|
Comparisons
|
|
|
|
-----------
|
|
|
|
|
|
|
|
Enumeration values are compared by identity::
|
|
|
|
|
|
|
|
>>> Colors.red is Colors.red
|
|
|
|
True
|
|
|
|
>>> Colors.blue is Colors.blue
|
|
|
|
True
|
|
|
|
>>> Colors.red is not Colors.blue
|
|
|
|
True
|
|
|
|
>>> Colors.blue is Colors.red
|
2013-02-23 09:36:35 -05:00
|
|
|
False
|
|
|
|
|
|
|
|
Ordered comparisons between enumeration values are *not* supported. Enums are
|
2013-04-04 09:52:14 -04:00
|
|
|
not integers (but see `IntEnum`_ below)::
|
2013-02-23 09:36:35 -05:00
|
|
|
|
|
|
|
>>> Colors.red < Colors.blue
|
|
|
|
Traceback (most recent call last):
|
|
|
|
...
|
|
|
|
NotImplementedError
|
|
|
|
>>> Colors.red <= Colors.blue
|
|
|
|
Traceback (most recent call last):
|
|
|
|
...
|
|
|
|
NotImplementedError
|
|
|
|
>>> Colors.blue > Colors.green
|
|
|
|
Traceback (most recent call last):
|
|
|
|
...
|
|
|
|
NotImplementedError
|
|
|
|
>>> Colors.blue >= Colors.green
|
|
|
|
Traceback (most recent call last):
|
|
|
|
...
|
|
|
|
NotImplementedError
|
|
|
|
|
2013-02-23 12:16:16 -05:00
|
|
|
Equality comparisons are defined though::
|
2013-02-23 09:36:35 -05:00
|
|
|
|
|
|
|
>>> Colors.blue == Colors.blue
|
|
|
|
True
|
|
|
|
>>> Colors.green != Colors.blue
|
|
|
|
True
|
|
|
|
|
2013-04-04 09:52:14 -04:00
|
|
|
Comparisons against non-enumeration values will always compare not equal::
|
2013-02-23 09:36:35 -05:00
|
|
|
|
|
|
|
>>> Colors.green == 2
|
|
|
|
False
|
|
|
|
>>> Colors.blue == 3
|
|
|
|
False
|
|
|
|
>>> Colors.green != 3
|
|
|
|
True
|
|
|
|
>>> Colors.green == 'green'
|
|
|
|
False
|
|
|
|
|
|
|
|
|
2013-04-04 09:52:14 -04:00
|
|
|
Extending enumerations by subclassing
|
|
|
|
-------------------------------------
|
2013-02-23 09:36:35 -05:00
|
|
|
|
2013-04-04 09:52:14 -04:00
|
|
|
You can extend previously defined Enums by subclassing::
|
2013-02-23 09:36:35 -05:00
|
|
|
|
2013-04-04 09:52:14 -04:00
|
|
|
>>> class MoreColors(Colors):
|
|
|
|
... pink = 4
|
|
|
|
... cyan = 5
|
|
|
|
|
|
|
|
When extended in this way, the base enumeration's values are identical to the
|
|
|
|
same named values in the derived class::
|
|
|
|
|
|
|
|
>>> Colors.red is MoreColors.red
|
|
|
|
True
|
|
|
|
>>> Colors.blue is MoreColors.blue
|
2013-02-23 09:36:35 -05:00
|
|
|
True
|
2013-04-02 10:09:55 -04:00
|
|
|
|
2013-04-05 13:52:37 -04:00
|
|
|
However, these are not doing comparisons against the integer
|
|
|
|
equivalent values, because if you define an enumeration with similar
|
|
|
|
item names and integer values, they will not be identical::
|
2013-04-02 10:09:55 -04:00
|
|
|
|
2013-04-04 09:52:14 -04:00
|
|
|
>>> class OtherColors(Enum):
|
|
|
|
... red = 1
|
|
|
|
... blue = 2
|
|
|
|
... yellow = 3
|
|
|
|
>>> Colors.red is OtherColors.red
|
|
|
|
False
|
|
|
|
>>> Colors.blue is not OtherColors.blue
|
2013-02-23 09:36:35 -05:00
|
|
|
True
|
2013-04-02 10:09:55 -04:00
|
|
|
|
2013-04-12 10:24:13 -04:00
|
|
|
Because ``Colors`` and ``OtherColors`` are unrelated enumerations,
|
|
|
|
their values are not equal, and thus they may exist in the same set,
|
|
|
|
or as distinct keys in the same dictionary::
|
2013-04-02 10:09:55 -04:00
|
|
|
|
2013-04-04 09:52:14 -04:00
|
|
|
>>> Colors.red == OtherColors.red
|
|
|
|
False
|
|
|
|
>>> len(set((Colors.red, OtherColors.red)))
|
|
|
|
2
|
2013-02-23 09:36:35 -05:00
|
|
|
|
2013-04-04 09:52:14 -04:00
|
|
|
You may not define two enumeration values with the same integer value::
|
2013-02-23 09:36:35 -05:00
|
|
|
|
|
|
|
>>> class Bad(Enum):
|
|
|
|
... cartman = 1
|
|
|
|
... stan = 2
|
|
|
|
... kyle = 3
|
|
|
|
... kenny = 3 # Oops!
|
|
|
|
... butters = 4
|
|
|
|
Traceback (most recent call last):
|
|
|
|
...
|
2013-04-09 23:32:13 -04:00
|
|
|
ValueError: Conflicting enums with value '3': 'kenny' and 'kyle'
|
2013-02-23 09:36:35 -05:00
|
|
|
|
2013-02-23 12:16:16 -05:00
|
|
|
You also may not duplicate values in derived enumerations::
|
2013-02-23 09:36:35 -05:00
|
|
|
|
|
|
|
>>> class BadColors(Colors):
|
|
|
|
... yellow = 4
|
|
|
|
... chartreuse = 2 # Oops!
|
|
|
|
Traceback (most recent call last):
|
|
|
|
...
|
2013-04-09 23:32:13 -04:00
|
|
|
ValueError: Conflicting enums with value '2': 'green' and 'chartreuse'
|
2013-02-23 09:36:35 -05:00
|
|
|
|
|
|
|
|
2013-04-04 09:52:14 -04:00
|
|
|
Enumeration values
|
|
|
|
------------------
|
|
|
|
|
|
|
|
The examples above use integers for enumeration values. Using integers is
|
|
|
|
short and handy (and provided by default by the `Convenience API`_), but not
|
|
|
|
strictly enforced. In the vast majority of use-cases, one doesn't care what
|
|
|
|
the actual value of an enumeration is. But if the value *is* important,
|
|
|
|
enumerations can have arbitrary values. The following example uses strings::
|
|
|
|
|
|
|
|
>>> class SpecialId(Enum):
|
|
|
|
... selector = '$IM($N)'
|
|
|
|
... adaptor = '~$IM'
|
2013-04-04 09:52:41 -04:00
|
|
|
...
|
2013-04-04 09:52:14 -04:00
|
|
|
>>> SpecialId.selector
|
|
|
|
<EnumValue: SpecialId.selector [value=$IM($N)]>
|
|
|
|
>>> SpecialId.selector.value
|
|
|
|
'$IM($N)'
|
|
|
|
>>> a = SpecialId.adaptor
|
|
|
|
>>> a == '~$IM'
|
|
|
|
False
|
|
|
|
>>> a == SpecialId.adaptor
|
|
|
|
True
|
|
|
|
>>> print(a)
|
|
|
|
SpecialId.adaptor
|
|
|
|
>>> print(a.value)
|
|
|
|
~$IM
|
2013-02-23 09:36:35 -05:00
|
|
|
|
2013-04-04 09:52:14 -04:00
|
|
|
Here ``Enum`` is used to provide readable (and syntactically valid!) names for
|
|
|
|
some special values, as well as group them together.
|
|
|
|
|
2013-04-05 13:52:37 -04:00
|
|
|
While ``Enum`` supports this flexibility, one should only use it in
|
|
|
|
very special cases. Code will be most readable when actual values of
|
|
|
|
enumerations aren't important and enumerations are just used for their
|
|
|
|
naming and comparison properties.
|
2013-02-23 09:36:35 -05:00
|
|
|
|
|
|
|
|
2013-03-15 10:25:45 -04:00
|
|
|
IntEnum
|
|
|
|
-------
|
|
|
|
|
2013-04-05 13:52:37 -04:00
|
|
|
A variation of ``Enum`` is proposed where the enumeration values also
|
|
|
|
subclasses ``int`` - ``IntEnum``. These values can be compared to
|
2013-04-10 00:18:55 -04:00
|
|
|
integers; by extension, enumerations of different types can also be
|
2013-04-05 13:52:37 -04:00
|
|
|
compared to each other::
|
2013-03-15 10:25:45 -04:00
|
|
|
|
|
|
|
>>> from enum import IntEnum
|
|
|
|
>>> class Shape(IntEnum):
|
|
|
|
... circle = 1
|
|
|
|
... square = 2
|
|
|
|
...
|
|
|
|
>>> class Request(IntEnum):
|
|
|
|
... post = 1
|
|
|
|
... get = 2
|
|
|
|
...
|
|
|
|
>>> Shape == 1
|
|
|
|
False
|
|
|
|
>>> Shape.circle == 1
|
|
|
|
True
|
|
|
|
>>> Shape.circle == Request.post
|
|
|
|
True
|
|
|
|
|
|
|
|
However they still can't be compared to ``Enum``::
|
|
|
|
|
|
|
|
>>> class Shape(IntEnum):
|
|
|
|
... circle = 1
|
|
|
|
... square = 2
|
|
|
|
...
|
|
|
|
>>> class Colors(Enum):
|
|
|
|
... red = 1
|
|
|
|
... green = 2
|
|
|
|
...
|
|
|
|
>>> Shape.circle == Colors.red
|
|
|
|
False
|
|
|
|
|
2013-04-05 13:52:37 -04:00
|
|
|
``IntEnum`` values behave like integers in other ways you'd expect::
|
2013-04-04 09:52:14 -04:00
|
|
|
|
|
|
|
>>> int(Shape.circle)
|
|
|
|
1
|
|
|
|
>>> ['a', 'b', 'c'][Shape.circle]
|
|
|
|
'b'
|
|
|
|
>>> [i for i in range(Shape.square)]
|
|
|
|
[0, 1]
|
|
|
|
|
2013-04-05 13:52:37 -04:00
|
|
|
For the vast majority of code, ``Enum`` is strongly recommended.
|
|
|
|
Since ``IntEnum`` breaks some semantic promises of an enumeration (by
|
|
|
|
being comparable to integers, and thus by transitivity to other
|
|
|
|
unrelated enumerations), it should be used only in special cases where
|
|
|
|
there's no other choice; for example, when integer constants are
|
|
|
|
replaced with enumerations and backwards compatibility is required
|
|
|
|
with code that still expects integers.
|
2013-03-15 10:25:45 -04:00
|
|
|
|
2013-04-04 09:52:14 -04:00
|
|
|
|
2013-02-23 09:36:35 -05:00
|
|
|
Pickling
|
|
|
|
--------
|
|
|
|
|
2013-02-23 12:16:16 -05:00
|
|
|
Enumerations created with the class syntax can also be pickled and unpickled::
|
2013-02-23 09:36:35 -05:00
|
|
|
|
|
|
|
>>> from enum.tests.fruit import Fruit
|
|
|
|
>>> from pickle import dumps, loads
|
|
|
|
>>> Fruit.tomato is loads(dumps(Fruit.tomato))
|
|
|
|
True
|
|
|
|
|
2013-04-04 09:52:14 -04:00
|
|
|
|
2013-02-23 09:36:35 -05:00
|
|
|
Convenience API
|
|
|
|
---------------
|
|
|
|
|
2013-04-04 09:52:14 -04:00
|
|
|
The ``Enum`` class is callable, providing the following convenience API::
|
2013-04-02 10:09:55 -04:00
|
|
|
|
2013-04-04 09:52:14 -04:00
|
|
|
>>> Animals = Enum('Animals', 'ant bee cat dog')
|
|
|
|
>>> Animals
|
|
|
|
<Animals {ant: 1, bee: 2, cat: 3, dog: 4}>
|
|
|
|
>>> Animals.ant
|
|
|
|
<EnumValue: Animals.ant [value=1]>
|
|
|
|
>>> Animals.ant.value
|
|
|
|
1
|
2013-02-23 09:36:35 -05:00
|
|
|
|
2013-04-04 09:52:14 -04:00
|
|
|
The semantics of this API resemble ``namedtuple``. The first argument of
|
|
|
|
the call to ``Enum`` is the name of the enumeration. The second argument is
|
|
|
|
a source of enumeration value names. It can be a whitespace-separated string
|
|
|
|
of names, a sequence of names or a sequence of 2-tuples with key/value pairs.
|
|
|
|
The last option enables assigning arbitrary values to enumerations; the others
|
|
|
|
auto-assign increasing integers starting with 1. A new class derived from
|
|
|
|
``Enum`` is returned. In other words, the above assignment to ``Animals`` is
|
|
|
|
equivalent to::
|
2013-02-23 09:36:35 -05:00
|
|
|
|
2013-04-04 09:52:14 -04:00
|
|
|
>>> class Animals(Enum):
|
|
|
|
... ant = 1
|
|
|
|
... bee = 2
|
|
|
|
... cat = 3
|
|
|
|
... dog = 4
|
2013-02-23 09:36:35 -05:00
|
|
|
|
2013-04-04 09:52:14 -04:00
|
|
|
Examples of alternative name/value specifications::
|
2013-02-23 09:36:35 -05:00
|
|
|
|
2013-04-04 09:52:14 -04:00
|
|
|
>>> Enum('Animals', ['ant', 'bee', 'cat', 'dog'])
|
|
|
|
<Animals {ant: 1, bee: 2, cat: 3, dog: 4}>
|
|
|
|
>>> Enum('Animals', (('ant', 'one'), ('bee', 'two'), ('cat', 'three'), ('dog', 'four')))
|
|
|
|
<Animals {dog: four, ant: one, cat: three, bee: two}>
|
2013-02-23 09:36:35 -05:00
|
|
|
|
2013-04-05 13:52:37 -04:00
|
|
|
The second argument can also be a dictionary mapping names to values::
|
|
|
|
|
|
|
|
>>> levels = dict(debug=10, info=20, warning=30, severe=40)
|
|
|
|
>>> Enum('Levels', levels)
|
|
|
|
<Levels {debug: 10, info: 20, warning: 30, severe: 40}>
|
|
|
|
|
2013-02-25 10:15:03 -05:00
|
|
|
|
2013-02-24 09:32:40 -05:00
|
|
|
Proposed variations
|
|
|
|
===================
|
|
|
|
|
|
|
|
Some variations were proposed during the discussions in the mailing list.
|
2013-02-25 10:15:03 -05:00
|
|
|
Here's some of the more popular ones.
|
|
|
|
|
2013-04-04 09:52:14 -04:00
|
|
|
|
2013-02-24 09:32:40 -05:00
|
|
|
Not having to specify values for enums
|
|
|
|
--------------------------------------
|
|
|
|
|
|
|
|
Michael Foord proposed (and Tim Delaney provided a proof-of-concept
|
|
|
|
implementation) to use metaclass magic that makes this possible::
|
|
|
|
|
|
|
|
class Color(Enum):
|
|
|
|
red, green, blue
|
|
|
|
|
|
|
|
The values get actually assigned only when first looked up.
|
|
|
|
|
|
|
|
Pros: cleaner syntax that requires less typing for a very common task (just
|
2013-02-25 10:15:03 -05:00
|
|
|
listing enumeration names without caring about the values).
|
2013-02-24 09:32:40 -05:00
|
|
|
|
|
|
|
Cons: involves much magic in the implementation, which makes even the
|
2013-02-25 10:15:03 -05:00
|
|
|
definition of such enums baffling when first seen. Besides, explicit is
|
|
|
|
better than implicit.
|
|
|
|
|
2013-04-04 09:52:14 -04:00
|
|
|
|
2013-02-24 09:32:40 -05:00
|
|
|
Using special names or forms to auto-assign enum values
|
|
|
|
-------------------------------------------------------
|
|
|
|
|
|
|
|
A different approach to avoid specifying enum values is to use a special name
|
|
|
|
or form to auto assign them. For example::
|
|
|
|
|
|
|
|
class Color(Enum):
|
|
|
|
red = None # auto-assigned to 0
|
|
|
|
green = None # auto-assigned to 1
|
|
|
|
blue = None # auto-assigned to 2
|
|
|
|
|
|
|
|
More flexibly::
|
|
|
|
|
|
|
|
class Color(Enum):
|
|
|
|
red = 7
|
|
|
|
green = None # auto-assigned to 8
|
|
|
|
blue = 19
|
|
|
|
purple = None # auto-assigned to 20
|
|
|
|
|
|
|
|
Some variations on this theme:
|
|
|
|
|
|
|
|
#. A special name ``auto`` imported from the enum package.
|
|
|
|
#. Georg Brandl proposed ellipsis (``...``) instead of ``None`` to achieve the
|
|
|
|
same effect.
|
|
|
|
|
|
|
|
Pros: no need to manually enter values. Makes it easier to change the enum and
|
|
|
|
extend it, especially for large enumerations.
|
|
|
|
|
2013-02-25 10:15:03 -05:00
|
|
|
Cons: actually longer to type in many simple cases. The argument of explicit
|
|
|
|
vs. implicit applies here as well.
|
|
|
|
|
2013-02-24 09:32:40 -05:00
|
|
|
|
|
|
|
Use-cases in the standard library
|
|
|
|
=================================
|
|
|
|
|
|
|
|
The Python standard library has many places where the usage of enums would be
|
|
|
|
beneficial to replace other idioms currently used to represent them. Such
|
|
|
|
usages can be divided to two categories: user-code facing constants, and
|
|
|
|
internal constants.
|
|
|
|
|
|
|
|
User-code facing constants like ``os.SEEK_*``, ``socket`` module constants,
|
2013-04-02 10:09:55 -04:00
|
|
|
decimal rounding modes and HTML error codes could require backwards
|
|
|
|
compatibility since user code may expect integers. ``IntEnum`` as described
|
|
|
|
above provides the required semantics; being a subclass of ``int``, it does not
|
|
|
|
affect user code that expects integers, while on the other hand allowing
|
|
|
|
printable representations for enumeration values::
|
|
|
|
|
|
|
|
>>> import socket
|
|
|
|
>>> family = socket.AF_INET
|
|
|
|
>>> family == 2
|
|
|
|
True
|
|
|
|
>>> print(family)
|
|
|
|
SocketFamily.AF_INET
|
2013-02-24 09:32:40 -05:00
|
|
|
|
|
|
|
Internal constants are not seen by user code but are employed internally by
|
2013-04-02 10:09:55 -04:00
|
|
|
stdlib modules. These can be implemented with ``Enum``. Some examples
|
|
|
|
uncovered by a very partial skim through the stdlib: ``binhex``, ``imaplib``,
|
|
|
|
``http/client``, ``urllib/robotparser``, ``idlelib``, ``concurrent.futures``,
|
|
|
|
``turtledemo``.
|
2013-02-24 09:32:40 -05:00
|
|
|
|
|
|
|
In addition, looking at the code of the Twisted library, there are many use
|
2013-02-25 10:15:03 -05:00
|
|
|
cases for replacing internal state constants with enums. The same can be said
|
|
|
|
about a lot of networking code (especially implementation of protocols) and
|
|
|
|
can be seen in test protocols written with the Tulip library as well.
|
|
|
|
|
2013-02-23 09:36:35 -05:00
|
|
|
|
|
|
|
Differences from PEP 354
|
|
|
|
========================
|
|
|
|
|
|
|
|
Unlike PEP 354, enumeration values are not defined as a sequence of strings,
|
|
|
|
but as attributes of a class. This design was chosen because it was felt that
|
|
|
|
class syntax is more readable.
|
|
|
|
|
|
|
|
Unlike PEP 354, enumeration values require an explicit integer value. This
|
|
|
|
difference recognizes that enumerations often represent real-world values, or
|
|
|
|
must interoperate with external real-world systems. For example, to store an
|
|
|
|
enumeration in a database, it is better to convert it to an integer on the way
|
|
|
|
in and back to an enumeration on the way out. Providing an integer value also
|
|
|
|
provides an explicit ordering. However, there is no automatic conversion to
|
|
|
|
and from the integer values, because explicit is better than implicit.
|
|
|
|
|
|
|
|
Unlike PEP 354, this implementation does use a metaclass to define the
|
|
|
|
enumeration's syntax, and allows for extended base-enumerations so that the
|
|
|
|
common values in derived classes are identical (a singleton model). While PEP
|
|
|
|
354 dismisses this approach for its complexity, in practice any perceived
|
|
|
|
complexity, though minimal, is hidden from users of the enumeration.
|
|
|
|
|
2013-04-05 14:25:16 -04:00
|
|
|
Unlike PEP 354, enumeration values should only be tested by identity
|
|
|
|
comparison. This is to emphasize the fact that enumeration values are
|
|
|
|
singletons, much like ``None``.
|
2013-02-23 09:36:35 -05:00
|
|
|
|
|
|
|
|
|
|
|
Acknowledgments
|
|
|
|
===============
|
|
|
|
|
2013-02-23 12:16:16 -05:00
|
|
|
This PEP describes the ``flufl.enum`` package by Barry Warsaw. ``flufl.enum``
|
|
|
|
is based on an example by Jeremy Hylton. It has been modified and extended
|
2013-02-25 10:15:03 -05:00
|
|
|
by Barry Warsaw for use in the GNU Mailman [5]_ project. Ben Finney is the
|
2013-02-23 12:16:16 -05:00
|
|
|
author of the earlier enumeration PEP 354.
|
2013-02-23 09:36:35 -05:00
|
|
|
|
2013-02-25 10:15:03 -05:00
|
|
|
|
2013-02-23 09:36:35 -05:00
|
|
|
References
|
|
|
|
==========
|
|
|
|
|
2013-02-25 10:15:03 -05:00
|
|
|
.. [1] http://pythonhosted.org/flufl.enum/docs/using.html
|
|
|
|
.. [2] http://www.python.org/dev/peps/pep-0354/
|
|
|
|
.. [3] http://mail.python.org/pipermail/python-ideas/2013-January/019003.html
|
|
|
|
.. [4] http://mail.python.org/pipermail/python-ideas/2013-February/019373.html
|
|
|
|
.. [5] http://www.list.org
|
|
|
|
|
2013-02-23 09:36:35 -05:00
|
|
|
|
|
|
|
Copyright
|
|
|
|
=========
|
|
|
|
|
|
|
|
This document has been placed in the public domain.
|
|
|
|
|
2013-02-25 10:15:03 -05:00
|
|
|
|
2013-02-23 09:36:35 -05:00
|
|
|
Todo
|
|
|
|
====
|
|
|
|
|
2013-02-23 12:16:16 -05:00
|
|
|
* Mark PEP 354 "superseded by" this one, if accepted
|
2013-02-23 09:36:35 -05:00
|
|
|
|
|
|
|
..
|
|
|
|
Local Variables:
|
|
|
|
mode: indented-text
|
|
|
|
indent-tabs-mode: nil
|
|
|
|
sentence-end-double-space: t
|
|
|
|
fill-column: 70
|
|
|
|
coding: utf-8
|
|
|
|
End:
|