Changes following discussion with Nick and Barry

This commit is contained in:
Eli Bendersky 2013-04-09 21:18:55 -07:00
parent abebe41b7b
commit dd1600e927
1 changed files with 5 additions and 32 deletions

View File

@ -97,8 +97,6 @@ Creating an Enum
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 @@ Enumeration values are hashable, so they can be used in dictionaries and sets::
>>> 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 @@ item names and integer values, they will not be identical::
>>> 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 @@ IntEnum
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