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

More Python tasks

Back to the full Python cheat sheet