Types
Enums
An enum member has a symbolic name and an underlying value. The symbolic name is what readers usually care about in code.
Source
from enum import Enum
class Status(Enum):
PENDING = "pending"
DONE = "done"
current = Status.PENDING
print(current.name)
print(current.value)Output
PENDING
pendingCompare enum members with enum members, not with raw strings. This keeps the set of valid states explicit.
Source
print(current is Status.PENDING)
print(current == "pending")Output
True
FalseNotes
- Enums make states and choices explicit.
- Members have names and values.
- Comparing enum members avoids string typo bugs.
- Prefer raw strings for open-ended text; prefer enums for a closed set of named choices.
See also
- prerequisite: Literals
- prerequisite: Classes
- related: Literal and Final
Run the complete example
Expected output
PENDING
pending
True
False
Execution time appears here after you run the example.