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
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.
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.
One of the many neat features of Python's Enum
s is retrieval by name:
>>> print(Numbering[key])
Numbering.b
and for the value:
>>> print(Numbering[key].value)
3
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With