# 枚举 Enum
from enum import Enum,unique
Month = Enum('Month', ('Jan', 'Feb', 'Mar', 'Apr', 'May'))
for name, member in Month.__members__.items():
print(name, '=>', member, ',', member.value)
# 枚举派生自定义类
@unique # 检查保证没有重复值
class Weekday(Enum):
Sun = 0 # Sun的Value被设定为0
Mon = 1
Tue = 2
Wed = 3
Thu = 4
Fri = 5
Sat = 6
print(Weekday.Sun)
print(Weekday['Sun'])
print(Weekday(1))
print(Weekday.Sun.value)
网友评论