Code Explanation:
1. Import Enum
from enum import Enum
You're importing the Enum class from Python's enum module.
Enum is used to create enumerations: named constants that are unique and immutable.
2. Define Enum Class
class Color(Enum):
RED = 1
GREEN = 2
This defines an enumeration named Color with two members:
Color.RED with value 1
Color.GREEN with value 2
3. Generator Function
def colors():
yield Color.RED
yield Color.GREEN
This is a generator function named colors.
It yields two enum members: Color.RED and Color.GREEN.
4. List Comprehension with .name
print([c.name for c in colors()])
This line:
Calls the colors() generator
Iterates through each yielded value
Accesses the .name attribute of each Color enum member
Color.RED.name → "RED"
Color.GREEN.name → "GREEN"
The result is a list of strings: ['RED', 'GREEN']
Final Output
['RED', 'GREEN']
.png)

0 Comments:
Post a Comment