Describe __members__ instead of __aliases__. +a few additional touch-ups

This commit is contained in:
Eli Bendersky 2013-05-08 20:32:27 -07:00
parent cc7de05c80
commit 3ab1885dc7
1 changed files with 22 additions and 9 deletions

View File

@ -59,16 +59,17 @@ Guido has pronounced a decision on this issue [5]_, as well as related issues
of not allowing to subclass enums [6]_, unless they define no enumeration
members [7]_.
Motivation
==========
*[Based partly on the Motivation stated in PEP 354]*
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.
set of constant values that may or may not have a 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.
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,
@ -193,7 +194,8 @@ Having two enum members with the same name is invalid::
However, two enum members are allowed to have the same value. Given two members
A and B with the same value (and A defined first), B is an alias to A. By-value
lookup of the value of A and B will return A.
lookup of the value of A and B will return A. By-name lookup of B will also
return A::
>>> class Shape(Enum):
... square = 2
@ -213,13 +215,24 @@ Iterating over the members of an enum does not provide the aliases::
>>> list(Shape)
[<Shape.square: 2>, <Shape.diamond: 1>, <Shape.circle: 3>]
The special attribute ``__aliases__`` contains a list of all enum
member aliases, by name::
The special attribute ``__members__`` is an ordered dictionary mapping names
to members. It includes all names defined in the enumeration, including the
aliases::
>>> Shape.__aliases__
>>> for name, member in Shape.__members__.items():
... name, member
...
('square', <Shape.square: 2>)
('diamond', <Shape.diamond: 1>)
('circle', <Shape.circle: 3>)
('alias_for_square', <Shape.square: 2>)
The ``__members__`` attribute can be used for detailed programmatic access to
the enumeration members. For example, finding all the aliases::
>>> [name for name, member in Shape.__members__.items() if member.name != name]
['alias_for_square']
Comparisons
-----------