Skip to content
Low Level Design Mastery Logo
LowLevelDesign Mastery

Enums

Create type-safe named constants with enums.

Enums (enumerations) are a way to define a set of named constants. They provide type safety and make code more readable by using meaningful names instead of magic numbers or strings.

An enum is a special type that represents a fixed set of constants. Instead of using arbitrary numbers or strings, enums allow you to use meaningful names.

  • Type Safety - Prevents invalid values
  • Readability - Code is self-documenting
  • Maintainability - Easy to update values in one place
  • IDE Support - Better autocomplete and refactoring

Python provides the Enum class from the enum module:

basic_enum.py
from enum import Enum
class Status(Enum):
"""Simple enum for status values"""
PENDING = "pending"
ACTIVE = "active"
INACTIVE = "inactive"
DELETED = "deleted"
# Usage
user_status = Status.ACTIVE
print(user_status) # Status.ACTIVE
print(user_status.value) # "active"
print(user_status.name) # "ACTIVE"
# Compare enum values
if user_status == Status.ACTIVE:
print("User is active")
Output
Status.ACTIVE
active
ACTIVE
User is active

Enums can use integer values:

int_enum.py
from enum import IntEnum
class Priority(IntEnum):
"""Enum with integer values"""
LOW = 1
MEDIUM = 2
HIGH = 3
CRITICAL = 4
# Can compare with integers
priority = Priority.HIGH
print(priority) # Priority.HIGH
print(priority.value) # 3
print(priority < Priority.CRITICAL) # True
print(priority == 3) # True (IntEnum allows this)
Output
Priority.HIGH
3
True
True

Enums can have methods and properties:

enum_with_methods.py
from enum import Enum
class Color(Enum):
"""Enum with methods"""
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
YELLOW = (255, 255, 0)
def __init__(self, r, g, b):
self.r = r
self.g = g
self.b = b
def rgb(self):
"""Return RGB tuple"""
return (self.r, self.g, self.b)
def hex(self):
"""Return hex color code"""
return f"#{self.r:02x}{self.g:02x}{self.b:02x}"
def is_dark(self):
"""Check if color is dark"""
return (self.r + self.g + self.b) < 400
# Usage
color = Color.RED
print(color.rgb()) # (255, 0, 0)
print(color.hex()) # #ff0000
print(color.is_dark()) # False
Output
(255, 0, 0)
#ff0000
False
order_status_enum.py
from enum import Enum, auto
class OrderStatus(Enum):
"""Order status enum"""
PENDING = auto()
CONFIRMED = auto()
PROCESSING = auto()
SHIPPED = auto()
DELIVERED = auto()
CANCELLED = auto()
def can_cancel(self):
"""Check if order can be cancelled"""
return self in [OrderStatus.PENDING, OrderStatus.CONFIRMED]
def is_completed(self):
"""Check if order is completed"""
return self == OrderStatus.DELIVERED
def next_status(self):
"""Get next status in workflow"""
status_flow = {
OrderStatus.PENDING: OrderStatus.CONFIRMED,
OrderStatus.CONFIRMED: OrderStatus.PROCESSING,
OrderStatus.PROCESSING: OrderStatus.SHIPPED,
OrderStatus.SHIPPED: OrderStatus.DELIVERED,
}
return status_flow.get(self)
class Order:
def __init__(self, order_id: str):
self.order_id = order_id
self.status = OrderStatus.PENDING
def advance_status(self):
"""Move to next status"""
next_status = self.status.next_status()
if next_status:
self.status = next_status
return f"Order {self.order_id} moved to {self.status.name}"
return f"Order {self.order_id} cannot advance from {self.status.name}"
def cancel(self):
"""Cancel order if possible"""
if self.status.can_cancel():
self.status = OrderStatus.CANCELLED
return f"Order {self.order_id} cancelled"
return f"Order {self.order_id} cannot be cancelled from {self.status.name}"
# Usage
order = Order("ORD-001")
print(order.status) # OrderStatus.PENDING
print(order.advance_status()) # Order ORD-001 moved to CONFIRMED
print(order.status) # OrderStatus.CONFIRMED
print(order.cancel()) # Order ORD-001 cancelled
Output
OrderStatus.PENDING
Order ORD-001 moved to CONFIRMED
OrderStatus.CONFIRMED
Order ORD-001 cancelled

You can iterate over all enum values:

enum_iteration.py
from enum import Enum
class Day(Enum):
MONDAY = 1
TUESDAY = 2
WEDNESDAY = 3
THURSDAY = 4
FRIDAY = 5
SATURDAY = 6
SUNDAY = 7
# Iterate over all enum values
for day in Day:
print(f"{day.name}: {day.value}")
# Check if value exists
print(Day.MONDAY in Day) # True
print(Day(1) == Day.MONDAY) # True
Output
MONDAY: 1
TUESDAY: 2
WEDNESDAY: 3
THURSDAY: 4
FRIDAY: 5
SATURDAY: 6
SUNDAY: 7
True
True
Diagram

Use enums when:

  • You have a fixed set of related constants
  • You want type safety (prevent invalid values)
  • You need self-documenting code
  • Values won’t change frequently
  • You want better IDE support

Examples:

  • Status values (PENDING, ACTIVE, INACTIVE)
  • Days of the week
  • Colors
  • Priority levels
  • Error codes
  • Configuration options