Get enumeration name by value

PythonEnumsEnumeration

Python Problem Overview


Are there any standard methods to get Enumeration names by value?

An example:

class Example(enum.Enum):
    one = 1
    two = 2

ex_variable = 1

Given ex_variable, can I obtain the string contained in Example.one.name?

Python Solutions


Solution 1 - Python

>>> Example(1).name
'one'

also see the Python docs.

Solution 2 - Python

To access the members of enums programatically:

>>> Example(ex_variable).name
'one'

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJilocView Question on Stackoverflow
Solution 1 - PythonNils WernerView Answer on Stackoverflow
Solution 2 - PythonAKSView Answer on Stackoverflow