Python Enum, IntEnum, StrEnum – Guide with Examples
Introduction – Why Use Enums in Python?
In many applications, you need a set of named constant values—for example, days of the week, status codes, or user roles.
Instead of using plain strings or integers, Python’s enum module helps you:
- Define human-readable constant values
- Ensure uniqueness and safety
- Group related values under one class
In this guide, you’ll learn:
- What Enums are and how they work
- How to create and access Enum members
- Enum types:
Enum,IntEnum,StrEnum - Best practices and common pitfalls
What Is an Enum?
An Enum (Enumeration) is a class that contains a set of constant members, each with a unique name and value.
Enums improve readability, safety, and code structure.
How to Define a Python Enum
➤ Import Enum from the enum module:
from enum import Enum
class Status(Enum):
PENDING = 1
APPROVED = 2
REJECTED = 3
Accessing Enum Members
print(Status.PENDING) # Status.PENDING
print(Status.PENDING.name) # PENDING
print(Status.PENDING.value) # 1
Enums are class members with unique name and value.
Iterating Over Enum Members
for status in Status:
print(status.name, status.value)
Output:
PENDING 1
APPROVED 2
REJECTED 3
Comparison and Identity
if Status.APPROVED == Status(2):
print("Approved!")
You can compare enums by value or by identity (is keyword).
Using IntEnum (Supports Integer Comparison)
from enum import IntEnum
class Priority(IntEnum):
LOW = 1
MEDIUM = 2
HIGH = 3
print(Priority.HIGH > Priority.LOW) # True
IntEnum members are compatible with integers in comparisons.
Python 3.11+: StrEnum for String Comparison
from enum import StrEnum
class Color(StrEnum):
RED = "red"
GREEN = "green"
BLUE = "blue"
print(Color.RED == "red") # True
Great when working with APIs or string-based configs.
Real-World Example – User Roles
from enum import Enum
class Role(Enum):
ADMIN = "admin"
MODERATOR = "moderator"
USER = "user"
def has_permission(role):
if role == Role.ADMIN:
return True
return False
print(has_permission(Role.ADMIN)) # True
print(has_permission(Role.USER)) # False
Common Pitfalls with Enums
| Mistake | Why It’s a Problem | Fix |
|---|---|---|
| Using plain strings for states | Leads to typos and inconsistency | Use Enums |
| Comparing enum to string directly | Enum != "VALUE" unless using StrEnum | Use Enum.value or StrEnum |
| Creating duplicate values without care | Can cause logical errors | Use @unique decorator |
Enforcing Unique Values with @unique
from enum import Enum, unique
@unique
class Status(Enum):
ACTIVE = 1
INACTIVE = 2
# ACTIVE = 2 would raise ValueError
Ensures no duplicate values in your Enum.
Best Practices
| Do This | Avoid This |
|---|---|
| Use Enums for related constants | Scattering constants across code |
Prefer StrEnum/IntEnum when needed | Comparing enum with incompatible types |
| Name Enums with PascalCase | Using lowercase enum class names |
Use @unique for validation | Allowing silent duplicates |
Summary – Recap & Next Steps
Enums in Python provide a structured, readable, and type-safe way to manage constants. They are a great replacement for primitive values in status codes, roles, and configuration flags.
Key Takeaways:
- Use Enums to define named constants
- Access with
.name,.value, and loop withfor - Use
IntEnumfor numeric comparison - Use
StrEnum(Python 3.11+) for string matching - Validate uniqueness with
@unique
Real-World Relevance:
Used in web apps, database status codes, API response types, and config-driven applications.
FAQ – Python Enums
What is an Enum in Python?
A class that defines a set of named constant values, grouped together under a common namespace.
Can I compare an Enum to a string or int?
Only if you’re using StrEnum or IntEnum. Otherwise, compare using .value.
Can I have duplicate values in an Enum?
Yes by default, but you can prevent this using @unique.
Can I iterate over enum members?
Yes:
for item in EnumClass:
print(item.name, item.value)
What’s the difference between Enum, IntEnum, and StrEnum?
| Type | Compatible with | Use Case |
|---|---|---|
Enum | enum members only | general-purpose enumerations |
IntEnum | integers | comparisons, sorting, numeric ops |
StrEnum | strings (Py 3.11+) | APIs, configs, serialization |
Share Now :
