[Python-checkins] peps: Describe __members__ instead of __aliases__. +a few additional touch-ups
eli.bendersky
python-checkins at python.org
Thu May 9 05:37:44 CEST 2013
http://hg.python.org/peps/rev/54ec32489684
changeset: 4877:54ec32489684
user: Eli Bendersky <eliben at gmail.com>
date: Wed May 08 20:32:27 2013 -0700
summary:
Describe __members__ instead of __aliases__. +a few additional touch-ups
files:
pep-0435.txt | 31 ++++++++++++++++++++++---------
1 files changed, 22 insertions(+), 9 deletions(-)
diff --git a/pep-0435.txt b/pep-0435.txt
--- a/pep-0435.txt
+++ b/pep-0435.txt
@@ -59,16 +59,17 @@
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 @@
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 @@
>>> 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
-----------
--
Repository URL: http://hg.python.org/peps
More information about the Python-checkins
mailing list