Reputation: 1663
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
Upvotes: 17
Views: 12948
Reputation: 69288
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
Upvotes: 21