在 Python 中将字符串与枚举进行比较
from enum import Enum, auto
class Signal(Enum):
red = auto()
green = auto()
orange = auto()
def equals(self, string):
return self.name == string
brain_detected_colour = "red"
print(Signal.red.equals(brain_detected_colour))
brain_detected_colour = "pink"
print(Signal.red.equals(brain_detected_colour))
Output:
True
False