Magic numbers and strings scattered through code are hard to maintain and prone to typos. Enumerations provide named constants with type safety, enabling IDE autocomplete, preventing invalid values, and making code more self-documenting.

Why Use Enums?

Iterating Over Enums

Enums are iterable and support lookup by value or name.

basic.py
# Basic enum definition

from enum import Enum

class Color(Enum):
    RED = 1
    GREEN = 2
    BLUE = 3

# Access enum members
color = 
print(color)           # Color member
print(color.name)      # Member name
print(color.value)     # Member value
print(type(color))     # Enum type
# Basic enum definition

from enum import Enum

class Color(Enum):
    RED = 1
    GREEN = 2
    BLUE = 3

# Access enum members
color = 
print(color)           # Color member
print(color.name)      # Member name
print(color.value)     # Member value
print(type(color))     # Enum type
# Basic enum definition

from enum import Enum

class Color(Enum):
    RED = 1
    GREEN = 2
    BLUE = 3

# Access enum members
color = 
print(color)           # Color member
print(color.name)      # Member name
print(color.value)     # Member value
print(type(color))     # Enum type
auto_values.py
# Auto-generating enum values

from enum import Enum, auto

class Status(Enum):
    PENDING = auto()
    APPROVED = auto()
    REJECTED = auto()
    CANCELLED = auto()

# Values are automatically assigned 1, 2, 3, 4
for status in Status:
    print(f"{status.name} = {status.value}")
string_enum.py
# String enums for API methods

from enum import Enum

class HttpMethod(Enum):
    GET = 'GET'
    POST = 'POST'
    PUT = 'PUT'
    DELETE = 'DELETE'
    PATCH = 'PATCH'

# Use in a function
def make_request(method: HttpMethod, url: str):
    print(f"{method.value} {url}")

make_request(HttpMethod.GET, "/api/users")
make_request(HttpMethod.POST, "/api/users")
comparison.py
# Comparing and checking enum values

from enum import Enum

class Priority(Enum):
    LOW = 1
    MEDIUM = 2
    HIGH = 3
    CRITICAL = 4

task_priority = Priority.HIGH

# Equality comparison
if task_priority == Priority.HIGH:
    print("High priority task!")

# Identity check (preferred, faster)
if task_priority is Priority.HIGH:
    print("Using identity check")

# Check membership
if Priority.CRITICAL in Priority:
    print("CRITICAL is a valid priority")
iteration.py
# Iterating over enums

from enum import Enum

class Day(Enum):
    MONDAY = 1
    TUESDAY = 2
    WEDNESDAY = 3
    THURSDAY = 4
    FRIDAY = 5
    SATURDAY = 6
    SUNDAY = 7

# Iterate over all days
print("All days:")
for day in Day:
    print(f"  {day.name}: {day.value}")

# Get enum by value
day = Day(3)
print(f"\nDay 3 is: {day.name}")

# Get enum by name
day = Day['FRIDAY']
print(f"Day named FRIDAY: {day.value}")

@seealso dataclass_intro "Dataclasses" @seealso typing_intro "Type hints"

Enum A class for creating enumerated constants with meaningful names and values, providing type safety and preventing the use of invalid values.
auto() A function that automatically generates the next integer value in an Enum, reducing manual value assignment errors.

Exercise: practical.py

Create a game state machine using Enum with MENU, PLAYING, PAUSED, and GAME_OVER states