Python:14.枚举类

2018-07-12  本文已影响3人  许瘦子来世
# 枚举 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)
上一篇 下一篇

猜你喜欢

热点阅读