python学习作业笔记十

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time     : 2018/8/18 17:09
# @Author   : 


# 枚举类型
from enum import Enum

# 自动赋值  默认从1 开始,且为int型
Month = Enum('Month', ('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'))
for name, value in Month.__members__.items():
    print(name, value)

# 假如需要更精确控制枚举值 ,就需要自定义枚举
from enum import unique

# @unique装饰器可以帮助我们检查保证没有重复值。
@unique
class Week(Enum):
    Monday = 0
    Tuesday = 1
    Wednesday = 2
    Thursday = 3
    Friday = 4
    Saturday = 5
    Sunday = 6

# 访问这些枚举类型可以有若干种方法
print(Week.Monday)  # -->Week.Monday
print(Week['Monday']) #--->Week.Monday
print(Week.Monday.value) # ---> 0
print(Week(1)) #--->Week.Tuesday
for name,value in Week.__members__.items():
    print(name,value)

猜你喜欢

转载自blog.csdn.net/QWERTY1994/article/details/81835829
今日推荐