python的枚举类 Enum

#枚举
from enum import Enum,unique

#1、默认枚举类
Month = Enum('Month',('Jan','Feb','Mar'))
for name,member in Month.__members__.items():
    print(name,'=>',member,',',member.value)  #value属性是自动赋给成员的int常量,默认从1开始记数

# Jan => Month.Jan , 1
# Feb => Month.Feb , 2
# Mar => Month.Mar , 3


#2、自定义枚举类
@unique  #保证不重复
class Weekday(Enum):
    Sun = 0
    Mon = 1
    Tue = 2
    Wed = 3
    Thu = 4
    Fri = 5
    Sat = 6
#访问这些枚举类型可以有若干种方法:
day1 = Weekday.Mon
print(day1) #Weekday.Mon
print(Weekday.Mon.value) #1,通过枚举常量获取value
print(Weekday(1)) #Weekday.Mon,通过value获取枚举常量

你可能感兴趣的:(python学习)