diff --git a/pep-0435.txt b/pep-0435.txt index dccd4e3a5..2f2e60657 100644 --- a/pep-0435.txt +++ b/pep-0435.txt @@ -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) [, , ] -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', ) + ('diamond', ) + ('circle', ) + ('alias_for_square', ) + +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 -----------