I’m new to python. Is it possible to get the value of an Enum
key from a variable key?
JavaScript
x
8
1
class Numbering(Enum):
2
a=2
3
b=3
4
5
key = "b"
6
print(Numbering.key)
7
#the result I want is 3
8
Advertisement
Answer
One of the many neat features of Python’s Enum
s is retrieval by name:
JavaScript
1
3
1
>>> print(Numbering[key])
2
Numbering.b
3
and for the value:
JavaScript
1
3
1
>>> print(Numbering[key].value)
2
3
3