Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to retrieve an Enum key via variable

I'm new to python. Is it possible to get the value of an Enum key from a variable key?

class Numbering(Enum):
 a=2
 b=3

key = "b"
print(Numbering.key)
#the result I want is 3
like image 638
Goku Avatar asked Jul 27 '17 04:07

Goku


People also ask

Can enum be used as variable value?

Enumerated Type Declaration to Create a Variable Similar to pre-defined data types like int and char, you can also declare a variable for enum and other user-defined data types. Here's how to create a variable for enum.

Can an enum have one value?

Description. An enumeration. A string object that can have only one value, chosen from the list of values 'value1', 'value2', ..., NULL or the special '' error value. In theory, an ENUM column can have a maximum of 65,535 distinct values; in practice, the real maximum depends on many factors.


1 Answers

One of the many neat features of Python's Enums is retrieval by name:

>>> print(Numbering[key])
Numbering.b

and for the value:

>>> print(Numbering[key].value)
3
like image 104
Ethan Furman Avatar answered Oct 18 '22 12:10

Ethan Furman