Menu

Adding attributes to Enum values?

Reddit r/learnpython·u/pachura3·7 months ago
#32v6YDjw
#color#enum#is_warm#auto#orange
Reading 0:00
15s threshold

Let's say I have enum Color and I would like it to have an additional attribute is_warm which would indicate whether given color is traditionally perceived as "warm" (red, orange, yellow...) or not.

class Color(Enum): RED = auto() ORANGE = auto() BLUE = auto() VIOLET = auto() print(Color.ORANGE.is_warm) # True print(Color.BLUE.is_warm) # False 

How to add attribute is_warm to enum Color? Obviously, I want this information to be passed to the constructor of Color, not to introduce some centralized map of all colors or a giant if...