1. 作用
列举常量,例如月份,周数
2. 操作
from enum import Enum, unique
Month = Enum('Month', ('Jan', 'Feb', 'Mar', 'Apr', 'May',
'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'))
@unique # 检查是否具有重复值
class Weekday(Enum):
Sun = 0 # Sun的value被设定为0
Mon = 1
Tue = 2
Wed = 3
Thu = 4
Fri = 5
Sat = 6
if __name__ == '__main__':
day1 = Weekday.Mon
print(day1)
day2 = Weekday['Tue']
print(day2)
day3 = Weekday.Wed.value
print(day3)
for name, member in Weekday.__members__.items():
print('{} => {}.'.format(name, member))
for name, member in Month.__members__.items():
print('{} => {}.'.format(name, member))