I want to check if some string value exists in the values set of some Enum. Here is what I do:
from enum import Enum
class Color(str, Enum):
RED = "red"
GREEN = "green"
YELLOW = "yellow"
s = "red"
# s = "blue"
if any(s == c.value for c in Color):
print(Color(s))
When I checked the documentation I found that:
The EnumMeta metaclass is responsible for providing the contains(), dir(), iter() and other methods that allow one to do things with an Enum class that fail on a typical class, such as list(Color) or some_enum_var in Color
But I want something different (checking existence for values). Is there a more pythonic way to solve this?