Enumerate variables with Enum!
Enum
is a way that Python enumerate variables. The enum
module allows for the creation of enumerated constants—unique, immutable data types that are useful for representing a fixed set of values. These values, which are usually related by their context, are known as enumeration members.
Enum provides…
- Uniqueness - Each member of an
Enum
is unique within its definition, meaning no two members can have the same value. Attempting to define two members with the same value will result in an error unless you explicitly allow aliases. - Immutability - Enum members are immutable. Once the
Enum
class is defined, you cannot change the members or their values. - Iterability and Comparability - Enum classes support iteration over their members and can be compared using identity and equality checks.
- Accessing Members - You can access enumeration members by their names or values:
- Auto - If you want to automatically assign values to enum members, you can use the
auto()
function from the same module:
|
|
If we just want to make sure them to be unique and automatically assigned, then use auto()
|
|
Or simply,
|
|
- However, this hard codes numbers, which can create an issue in the future.
Iterating over Enum Members
You can iterate over the members of an enum:
|
|
Comparison of Enum Members
Enum members are singleton objects, so comparison is possible by identity:
|
|
Using Enum as a Type Hint
Enums can be used as type hints, enhancing code readability and correctness:
|
|
Extending Enums: IntEnum and StrEnum
For enums where the members are specifically integers or strings, you can inherit from IntEnum
or StrEnum
for additional benefits, like being able to compare members to integers or strings directly.
|
|
Unique Constraint
To ensure that all enum values are unique, you can use the @unique
decorator:
|
|
Using @unique
will raise a ValueError
if any duplicate values are detected.
Conclusion
Enums in Python are useful for defining sets of named constants that are related and have a fixed set of members. They improve code readability, prevent errors related to using incorrect literal values, and can simplify type checking and validation in your programs.