Module 17 · Advanced Python Concepts — topic 14 of 14
Enums
Named constant sets with enum: Enum members, names and values, lookup by value and name, auto(), iteration, methods on enums, StrEnum and Flag, and using enums for state machines.
Learning objectives
- Define and use Enum classes
- Look up members by name and value
- Use StrEnum, Flag and enum methods
Definition
An enumeration (enum) is a class whose members are a fixed set of named constants. enum.Enum members have a name and a value, compare by identity, and can be iterated, looked up and given methods.
Introduction
Order states, colours, user roles — when a value must be one of a few options, an enum is safer and clearer than loose strings like "shiped" (typo included).
Why it matters
Enums catch typos early, document the allowed values in one place and make code easier to read and refactor.
Detailed explanation
Define members as class attributes: class Color(Enum): RED = 1. Members are singletons: Color.RED is Color.RED.
Each member has .name ("RED") and .value (1). Color(1) looks up by value; Color["RED"] by name; invalid lookups raise ValueError or KeyError.
auto() assigns values automatically when the actual values don't matter.
Enums are iterable (for c in Color) and len(Color) counts members. Plain Enum members are not equal to their raw values (Status.PENDING == 1 is False).
Enums can have methods and properties, keeping behaviour next to the constants.
StrEnum and IntEnum members behave like strings or ints, convenient for JSON and databases. Flag supports combinations with | and membership tests with in.
Enums suit state machines: each state knows which transitions are allowed.
Syntax
- Members are named constants.
- Look up by value or name.
- auto() for automatic values.
Code examples
Members, names and values
Color.RED RED 1 Color.GREEN Color.BLUE 3
auto(), iteration and comparison
['PENDING', 'SHIPPED', 'DELIVERED'] 2 True False
More examples
A state machine, StrEnum and Flag
cannot go from new to shipped -> paid -> shipped cannot go from shipped to new True level=low True False Perm.READ|WRITE
Step-by-step explanation
- can_move_to keeps the transition rules inside the enum, so invalid moves are rejected in one place.
- StrEnum members equal their lower-case string values, which is handy for JSON and databases.
- Flag members combine with | and are tested with in.
Expected output
Running “A state machine, StrEnum and Flag” prints exactly:
Practical applications
- Order and payment statuses in e-commerce.
- File permissions and feature flags.
Best practices
- Use enums for fixed sets of options.
- Compare members, not raw values.
- Use StrEnum when values are stored as text.
Common mistakes
Comparing members with raw values
Avoid
Prefer
Plain Enum members don't equal their values; compare members.
Important notes
- match statements work well with enums: case OrderState.PAID: ...
Key takeaways
- Enums define named constants with name and value.
- Look up with Enum(value) and Enum[name].
- StrEnum, IntEnum and Flag cover special needs.
Try it yourself
- 1Create a TrafficLight enum with a next() method cycling RED → GREEN → YELLOW → RED.
- 2Model user roles with Flag and check whether a user can edit.
Finished reading? Mark it complete to update your dashboard.
Suggested practice
1 question test this exact topic. Answers and explanations are revealed after you submit.
- AdvancedWhat is printed?