I just discovered the existence of an Enum base class in python and I'm trying to imagine how it could be useful to me.
Let's say I define a traffic light status:
from enum import Enum, auto
class Signal(Enum):
red = auto()
green = auto()
orange = auto()
Let's say I receive information from some subsystem in my program, in the form of a string representing a colour name, for instance brain_detected_colour = "red"
.
How do I compare this string to my traffic light signals?
Obviously, brain_detected_colour is Signal.red
is False
, because Signal.red
is not a string.
Signal(brain_detected_colour) is Signal.red
fails with ValueError: 'red' is not a valid Signal
.