mirror of
https://github.com/python/cpython.git
synced 2025-11-02 11:08:57 +00:00
Added example to enum docs show access to name and value attributes of enum members.
This commit is contained in:
parent
cf873c699f
commit
3fe70b4acc
1 changed files with 10 additions and 2 deletions
|
|
@ -87,8 +87,8 @@ Enumeration members are hashable, so they can be used in dictionaries and sets::
|
||||||
True
|
True
|
||||||
|
|
||||||
|
|
||||||
Programmatic access to enumeration members
|
Programmatic access to enumeration members and their attributes
|
||||||
------------------------------------------
|
---------------------------------------------------------------
|
||||||
|
|
||||||
Sometimes it's useful to access members in enumerations programmatically (i.e.
|
Sometimes it's useful to access members in enumerations programmatically (i.e.
|
||||||
situations where ``Color.red`` won't do because the exact color is not known
|
situations where ``Color.red`` won't do because the exact color is not known
|
||||||
|
|
@ -106,6 +106,14 @@ If you want to access enum members by *name*, use item access::
|
||||||
>>> Color['green']
|
>>> Color['green']
|
||||||
<Color.green: 2>
|
<Color.green: 2>
|
||||||
|
|
||||||
|
If have an enum member and need its :attr:`name` or :attr:`value`::
|
||||||
|
|
||||||
|
>>> member = Color.red
|
||||||
|
>>> member.name
|
||||||
|
'red'
|
||||||
|
>>> member.value
|
||||||
|
1
|
||||||
|
|
||||||
|
|
||||||
Duplicating enum members and values
|
Duplicating enum members and values
|
||||||
-----------------------------------
|
-----------------------------------
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue