Code Explanation:
1) from enum import Enum
Imports the base Enum class from Python’s enum module.
Enum lets you define named, constant members with unique identities.
2) class Color(Enum):
Starts an enumeration named Color.
Subclassing Enum means attributes defined inside become enum members, not plain class attributes.
3) RED = 1
Defines an enum member Color.RED with the underlying value 1.
RED is a singleton member; comparisons are by identity (Color.RED is Color.RED is True).
4) BLUE = 2
Defines another enum member Color.BLUE with value 2.
5) print(Color.RED.name, Color.RED.value)
Color.RED accesses the RED member.
.name → the member’s identifier string: "RED".
.value → the member’s underlying value: 1.
print prints them separated by a space (default sep=" ").
Output
RED 1
Download Book - 500 Days Python Coding Challenges with Explanation


0 Comments:
Post a Comment