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
Enumis 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
Enumclass 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:
from enum import Enum
class State(Enum):
PLAYING=0
PAUSED=1
GAME_OVER=2If we just want to make sure them to be unique and automatically assigned, then use auto()