Python Enum: Defining Enumerated Constants

Python’s built-in enum module provides a comprehensive suite of classes and decorators designed to create strongly typed, named constants. This article explores the core features of the enum module, detailing its primary base classes—including Enum, IntEnum, StrEnum, Flag, and IntFlag—as well as essential utilities like auto() and the @unique decorator that ensure data integrity, readability, and type safety across your code.

Core Enumeration Classes

The enum module introduces several base classes tailored to different use cases:

Bitwise and Flag Support

For scenarios where constants represent combinations of states or permissions, the module offers flag-based classes:

Key Utilities and Decorators

The enum module includes several helper tools to streamline definition and enforce strict constraints:

Functional Capabilities

Beyond simple name-value binding, enumerations created with the enum module offer built-in functionality: