[Python-checkins] peps: Changes following discussion with Nick and Barry
eli.bendersky
python-checkins at python.org
Wed Apr 10 06:19:45 CEST 2013
http://hg.python.org/peps/rev/1b6452d05925
changeset: 4845:1b6452d05925
parent: 4843:07e369037b19
user: Eli Bendersky <eliben at gmail.com>
date: Tue Apr 09 21:18:55 2013 -0700
summary:
Changes following discussion with Nick and Barry
files:
pep-0435.txt | 37 +++++--------------------------------
1 files changed, 5 insertions(+), 32 deletions(-)
diff --git a/pep-0435.txt b/pep-0435.txt
--- a/pep-0435.txt
+++ b/pep-0435.txt
@@ -97,8 +97,6 @@
Enumerations are created using the class syntax, which makes them easy to read
and write. An alternative creation method is described in `Convenience API`_.
-Every enumeration value must have a unique integer value and the
-only restriction on their names is that they must be valid Python identifiers.
To define an enumeration, derive from the ``Enum`` class and add attributes
with assignment to their integer values::
@@ -169,36 +167,10 @@
>>> apples
{<EnumValue: Colors.green [value=2]>: 'granny smith', <EnumValue: Colors.red [value=1]>: 'red delicious'}
-You can convert back to the enumeration value by indexing into the
-``Enum`` subclass, passing in the integer value for the item you want::
+To programmatically access enumeration values, use ``getattr``::
- >>> Colors[1]
+ >>> getattr(Colors, 'red')
<EnumValue: Colors.red [value=1]>
- >>> Colors[2]
- <EnumValue: Colors.green [value=2]>
- >>> Colors[3]
- <EnumValue: Colors.blue [value=3]>
- >>> Colors[1] is Colors.red
- True
-
-The string name of the enumeration value is also accepted::
-
- >>> Colors['red']
- <EnumValue: Colors.red [value=1]>
- >>> Colors['blue'] is Colors.blue
- True
-
-You get exceptions though, if you try to use invalid arguments::
-
- >>> Colors['magenta']
- Traceback (most recent call last):
- ...
- ValueError: magenta
- >>> Colors[99]
- Traceback (most recent call last):
- ...
- ValueError: 99
-
Comparisons
-----------
@@ -283,7 +255,8 @@
>>> Colors.blue is not OtherColors.blue
True
-These enumeration values are not equal, nor do they hash equally::
+These enumeration values are not equal, nor do they and hence may exist
+in the same set, or as distinct keys in the same dictionary::
>>> Colors.red == OtherColors.red
False
@@ -353,7 +326,7 @@
A variation of ``Enum`` is proposed where the enumeration values also
subclasses ``int`` - ``IntEnum``. These values can be compared to
-integers; by extensions, enumerations of different types can also be
+integers; by extension, enumerations of different types can also be
compared to each other::
>>> from enum import IntEnum
--
Repository URL: http://hg.python.org/peps
More information about the Python-checkins
mailing list