python学习45:枚举类

 枚举的作用就是为了在使用或者阅读某个变量时,可以一眼知道它代表的意思。

from enum import Enum
Month = Enum('Month',('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'))

for name,member in Month.__members__.items():
    print(name,'=>',member,',',member.value)

print("----------------------------")
from enum import Enum,unique

@unique
class Weekday(Enum):
    Mon = 1
    Tue = 2
    Wed = 3
    Thu = 4
    Fri = 5
    Sta = 6
    Sun = 7

for name,member in Weekday.__members__.items():
    print(name,'=>',member,',',member.value)

运算结果:

============================ RESTART: E:/slot.py ============================
Jan => Month.Jan , 1
Feb => Month.Feb , 2
Mar => Month.Mar , 3
Apr => Month.Apr , 4
May => Month.May , 5
Jun => Month.Jun , 6
Jul => Month.Jul , 7
Aug => Month.Aug , 8
Sep => Month.Sep , 9
Oct => Month.Oct , 10
Nov => Month.Nov , 11
Dec => Month.Dec , 12
----------------------------
Mon => Weekday.Mon , 1
Tue => Weekday.Tue , 2
Wed => Weekday.Wed , 3
Thu => Weekday.Thu , 4
Fri => Weekday.Fri , 5
Sta => Weekday.Sta , 6
Sun => Weekday.Sun , 7

 

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