diff options
author | Ethan Furman <ethan@stoneleaf.us> | 2015-11-20 21:12:26 (GMT) |
---|---|---|
committer | Ethan Furman <ethan@stoneleaf.us> | 2015-11-20 21:12:26 (GMT) |
commit | 748dad5b6a54370c72029afea9e7baa683d1ffe8 (patch) | |
tree | c96349e56a536dc7d4d44199ad8b7676ecbf5225 /Doc/library/enum.rst | |
parent | 5444de93ac3ff44ce6f8498ba77e6eb7c4a9d772 (diff) | |
download | cpython-748dad5b6a54370c72029afea9e7baa683d1ffe8.zip cpython-748dad5b6a54370c72029afea9e7baa683d1ffe8.tar.gz cpython-748dad5b6a54370c72029afea9e7baa683d1ffe8.tar.bz2 |
Close 25594: advise against accessing Enum members from other members
Diffstat (limited to 'Doc/library/enum.rst')
-rw-r--r-- | Doc/library/enum.rst | 26 |
1 files changed, 16 insertions, 10 deletions
diff --git a/Doc/library/enum.rst b/Doc/library/enum.rst index 0fbbf5a..a76f5a3 100644 --- a/Doc/library/enum.rst +++ b/Doc/library/enum.rst @@ -730,18 +730,24 @@ member instances. 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:: - - >>> Color.red - <Color.red: 1> - >>> Color.red.blue - Traceback (most recent call last): +: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:: + + >>> 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 + +.. versionchanged:: 3.5 -Likewise, the :attr:`__members__` is only available on the class. +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, |