Saturday, 11 September 2021

Python: Print all the members of enum

Using for-each loop, we can print all the members of an enumeration.

 

Example

for day in Day:
    print(day)

 

helloWorld.py

from enum import Enum

class Day(Enum):
    MONDAY = 1
    TUESDAY = 2
    WEDNESDAY = 3
    THURSDAY = 4
    FRIDAY = 5
    SATURDAY = 6
    SUNDAY = 7

for day in Day:
    print(day)

 

Output

Day.MONDAY
Day.TUESDAY
Day.WEDNESDAY
Day.THURSDAY
Day.FRIDAY
Day.SATURDAY
Day.SUNDAY

 


 

Previous                                                    Next                                                    Home

No comments:

Post a Comment