[Python-checkins] cpython: Close 25594: advise against accessing Enum members from other members

ethan.furman python-checkins at python.org
Fri Nov 20 16:12:46 EST 2015


https://hg.python.org/cpython/rev/f4b495ceab17
changeset: 99246:f4b495ceab17
parent: 99244:af1903ab6b1f
user: Ethan Furman <ethan at stoneleaf.us>
date: Fri Nov 20 13:12:26 2015 -0800
summary:
 Close 25594: advise against accessing Enum members from other members
files:
 Doc/library/enum.rst | 24 +++++++++++++++---------
 1 files changed, 15 insertions(+), 9 deletions(-)
diff --git a/Doc/library/enum.rst b/Doc/library/enum.rst
--- a/Doc/library/enum.rst
+++ b/Doc/library/enum.rst
@@ -730,18 +730,24 @@
 Finer Points
 ^^^^^^^^^^^^
 
-Enum members are instances of an Enum class, and even though they are
-accessible as `EnumClass.member`, they are not accessible directly from
-the member::
+:class:`Enum` members are instances of an :class:`Enum` class, and even
+though they are accessible as `EnumClass.member`, they should not be accessed
+directly from the member as that lookup may fail or, worse, return something
+besides the :class:`Enum` member you looking for::
 
- >>> Color.red
- <Color.red: 1>
- >>> Color.red.blue
- Traceback (most recent call last):
+ >>> class FieldTypes(Enum):
+ ... name = 0
+ ... value = 1
+ ... size = 2
 ...
- AttributeError: 'Color' object has no attribute 'blue'
+ >>> FieldTypes.value.size
+ <FieldTypes.size: 2>
+ >>> FieldTypes.size.value
+ 2
 
-Likewise, the :attr:`__members__` is only available on the class.
+.. versionchanged:: 3.5
+
+The :attr:`__members__` attribute is only available on the class.
 
 If you give your :class:`Enum` subclass extra methods, like the `Planet`_
 class above, those methods will show up in a :func:`dir` of the member,
-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list

AltStyle によって変換されたページ (->オリジナル) /