A few PEP 435 clarifications.

This commit is contained in:
Barry Warsaw 2013-05-06 16:15:38 -04:00
parent 939f5b14ff
commit cc7de05c80
1 changed files with 19 additions and 10 deletions

View File

@ -213,8 +213,8 @@ Iterating over the members of an enum does not provide the aliases::
>>> list(Shape) >>> list(Shape)
[<Shape.square: 2>, <Shape.diamond: 1>, <Shape.circle: 3>] [<Shape.square: 2>, <Shape.diamond: 1>, <Shape.circle: 3>]
If access to aliases is required for some reason, use the special attribute The special attribute ``__aliases__`` contains a list of all enum
``__aliases__``:: member aliases, by name::
>>> Shape.__aliases__ >>> Shape.__aliases__
['alias_for_square'] ['alias_for_square']
@ -244,6 +244,8 @@ Equality comparisons are defined though::
>>> Color.blue == Color.red >>> Color.blue == Color.red
False False
>>> Color.blue != Color.red
True
>>> Color.blue == Color.blue >>> Color.blue == Color.blue
True True
@ -294,7 +296,7 @@ Then::
The rules for what is allowed are as follows: all attributes defined within an The rules for what is allowed are as follows: all attributes defined within an
enumeration will become members of this enumeration, with the exception of enumeration will become members of this enumeration, with the exception of
*__dunder__* names and descriptors; methods are descriptors too. *__dunder__* names and descriptors [9]_; methods are descriptors too.
Restricted subclassing of enumerations Restricted subclassing of enumerations
@ -418,13 +420,19 @@ The ``Enum`` class is callable, providing the following functional API::
[<Animal.ant: 1>, <Animal.bee: 2>, <Animal.cat: 3>, <Animal.dog: 4>] [<Animal.ant: 1>, <Animal.bee: 2>, <Animal.cat: 3>, <Animal.dog: 4>]
The semantics of this API resemble ``namedtuple``. The first argument of 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 the call to ``Enum`` is the name of the enumeration. This argument
a source of enumeration member names. It can be a whitespace-separated string can be the short name of the enum or a dotted-name including the
of names, a sequence of names or a sequence of 2-tuples with key/value pairs. module path to better support pickling. E.g.::
The last option enables assigning arbitrary values to enumerations; the others
auto-assign increasing integers starting with 1. A new class derived from >>> Animals = Enum('animals.Animals', 'ant bee cat dog')
``Enum`` is returned. In other words, the above assignment to ``Animal`` is
equivalent to:: The second argument is the *source* of enumeration member names. It can be a
whitespace-separated string of names, a sequence of names, a sequence of
2-tuples with key/value pairs, or a mapping (e.g. dictionary) of names to
values. The last two options enable 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 ``Animal`` is equivalent to::
>>> class Animals(Enum): >>> class Animals(Enum):
... ant = 1 ... ant = 1
@ -570,6 +578,7 @@ References
.. [6] http://mail.python.org/pipermail/python-dev/2013-April/125716.html .. [6] http://mail.python.org/pipermail/python-dev/2013-April/125716.html
.. [7] http://mail.python.org/pipermail/python-dev/2013-May/125859.html .. [7] http://mail.python.org/pipermail/python-dev/2013-May/125859.html
.. [8] http://pythonhosted.org/flufl.enum/ .. [8] http://pythonhosted.org/flufl.enum/
.. [9] http://docs.python.org/3/howto/descriptor.html
Copyright Copyright
========= =========