Enums in Python
From the Python cheat sheet ยท Classes & OOP ยท verified Jul 2026
Enums
Named constants grouped under a single type.
python
from enum import Enum, auto
class Color(Enum):
RED = 1
GREEN = 2
BLUE = 3
Color.RED # <Color.RED: 1>
Color.RED.name # "RED"
Color.RED.value # 1
Color(1) # <Color.RED: 1> (lookup by value)
Color["RED"] # <Color.RED: 1> (lookup by name)
# auto() โ assign values automatically
class Status(Enum):
PENDING = auto()
ACTIVE = auto()
DONE = auto()๐ก Use Enum to give names to a small fixed set of values
โก IntEnum/StrEnum members ARE ints/strs โ compare them directly with literals
๐ auto() saves you from manually assigning incrementing values
๐ข Flag enums combine with | and check membership with in