Code Explanation:
๐น 1. Importing Enum and auto
from enum import Enum, auto
✅ Explanation
Enum and auto are imported from Python's built-in enum module.
Enum is used to create a collection of named constant values.
auto() automatically assigns values to enum members.
enum Module
│
▼
┌────────────┐
│ Enum │
│ auto() │
└────────────┘
Nothing executes yet.
๐น 2. Creating an Enum Class
class Day(Enum):
✅ Explanation
A new enumeration named Day is created.
Unlike a normal class:
Every variable inside becomes an Enum Member.
Enum members are constant values.
Current Memory
Day
↓
Enum Class
No members are assigned yet.
๐น 3. Creating the First Enum Member
MON = auto()
✅ Explanation
auto() automatically assigns the first integer value.
Since MON is the first member,
MON = 1
Current Memory
Day
MON → 1
๐น 4. Creating the Second Enum Member
TUE = auto()
✅ Explanation
auto() assigns the next available integer.
Since MON already has value 1,
TUE = 2
Current Memory
Day
MON → 1
TUE → 2
Visual Representation
Day
│
├── MON → 1
└── TUE → 2
๐น 5. Accessing an Enum Member
Day.TUE
✅ Explanation
Python accesses the enum member named TUE.
Current Memory
Day
↓
TUE
The object is
Day.TUE
๐น 6. Accessing .value
Day.TUE.value
✅ Explanation
Every Enum member has two important properties:
.name
.value
Current Memory
Day.TUE
↓
name = "TUE"
value = 2
Python returns
2
๐น 7. Printing the Result
print(Day.TUE.value)
✅ Explanation
Python prints the integer value assigned to TUE.
Output
2
๐ฏ Final Output
2

0 Comments:
Post a Comment