172

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?

Jiloc
  • 3,338
  • 3
  • 24
  • 38

2 Answers2

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

also see the Python docs.

Nils Werner
  • 34,832
  • 7
  • 76
  • 98
43

To access the members of enums programatically:

>>> Example(ex_variable).name
'one'
AKS
  • 18,983
  • 3
  • 43
  • 54