My end goal really is to create a helper method in my Enum class that always returns an Enum member and never raises an exception, given whatever possible value, e.g.
Color.from_value('red')
In case the value is not part of the enum, the helper method will return a default one, say Color.UNKNOWN
.
Based on this answer from another related question, I can do checks on the values by listing them through some built-in members. However, what I want to do next is to keep track of all the values in an internal member so that I don't have to always iterate through the values every time the helper method is called. I tried doing something similar to the following:
class Color(Enum):
RED = 'red'
BLUE = 'blue'
GREEN = 'green'
# this doesn't work
_values = [item.value for item in Color]
and as expected, it doesn't work. Is this perhaps something that's already built-in in Python Enums?