Get enumeration name by value

Jiloc picture Jiloc · Aug 2, 2016 · Viewed 49.5k times · Source

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?

Answer

Nils Werner picture Nils Werner · Aug 2, 2016
>>> Example(1).name
'one'

also see the Python docs.