Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 748dad5

Browse files
committed
Close 25594: advise against accessing Enum members from other members
1 parent 5444de9 commit 748dad5

1 file changed

Lines changed: 16 additions & 10 deletions

File tree

Doc/library/enum.rst

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -730,18 +730,24 @@ member instances.
730730
Finer Points
731731
^^^^^^^^^^^^
732732

733-
Enum members are instances of an Enum class, and even though they are
734-
accessible as `EnumClass.member`, they are not accessible directly from
735-
the member::
736-
737-
>>> Color.red
738-
<Color.red: 1>
739-
>>> Color.red.blue
740-
Traceback (most recent call last):
733+
:class:`Enum` members are instances of an :class:`Enum` class, and even
734+
though they are accessible as `EnumClass.member`, they should not be accessed
735+
directly from the member as that lookup may fail or, worse, return something
736+
besides the :class:`Enum` member you looking for::
737+
738+
>>> class FieldTypes(Enum):
739+
... name = 0
740+
... value = 1
741+
... size = 2
741742
...
742-
AttributeError: 'Color' object has no attribute 'blue'
743+
>>> FieldTypes.value.size
744+
<FieldTypes.size: 2>
745+
>>> FieldTypes.size.value
746+
2
747+
748+
.. versionchanged:: 3.5
743749

744-
Likewise, the :attr:`__members__` is only available on the class.
750+
The :attr:`__members__` attribute is only available on the class.
745751

746752
If you give your :class:`Enum` subclass extra methods, like the `Planet`_
747753
class above, those methods will show up in a :func:`dir` of the member,

0 commit comments

Comments
 (0)