Code Explanation:
1. Importing Enum and auto
from enum import Enum, auto
Enum: This is a base class used to create enumerations (named constant values).
auto: A helper function that automatically assigns values to enumeration members.
2. Defining the Enum Class
class Status(Enum):
This creates a new enumeration named Status.
It inherits from Enum, meaning each member of Status will be an enumeration value.
3. Adding Enum Members
STARTED = auto()
RUNNING = auto()
STOPPED = auto()
These are members of the Status enum.
auto() automatically assigns them integer values, starting from 1 by default (unless overridden).
STARTED will be 1
RUNNING will be 2
STOPPED will be 3
This is equivalent to:
STARTED = 1
RUNNING = 2
STOPPED = 3
but using auto() is cleaner and avoids manual numbering errors.
4. Accessing a Member's Value
print(Status.RUNNING.value)
Status.RUNNING accesses the RUNNING member of the enum.
.value gets the actual value assigned to it by auto(), which in this case is 2.
The output will be:
2
Final Output:
2
.png)

0 Comments:
Post a Comment