Answer based on question edit:
The functionality you're after is built in:
>>> Animal["DOG"].value "This is Dog"
or
>>> member = "DOG" >>> Animal[member].value "This is Dog"
The only reason to make that a function would be for error handling -- for instance, if you want to gracefully handle a non-member name being passed in.
I see two possible questions (the names of your functions and usage of terminology are confusing):
- How to get the
.value property; - How to get the value of any property (the two built-in are
value and name)
Actually, I know also see a third option:
- How to get the value of property whose name is passed in (so
get_enum_value is a method of Animal, not a separate function as your indentation would suggest)
Answering only the last question, the solution is something like:
import enum class Animal(enum.Enum): # DOG = "This is Dog" CAT = "This is cat" LION = "This is Lion" # def get_enum_value(self, enum_property_name): return getattr(self, enum_property_name)
and in use:
>>> Animal.LION.get_enum_value('name') 'LION'
enum property?DOGis an enum member, whilenameis an enum property, as isvalue