Saturday, 11 September 2021

Python: get enum member value

Using the ‘value’ property, you can get the enum member value.

 

enumMemberValue.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.name, '->', day.value)

 

Output

MONDAY -> 1
TUESDAY -> 2
WEDNESDAY -> 3
THURSDAY -> 4
FRIDAY -> 5
SATURDAY -> 6
SUNDAY -> 7

 

 

 

 

Previous                                                    Next                                                    Home

No comments:

Post a Comment