python的枚举

#coding=utf-8
from enum import  Enum,unique
@unique
class Weekday(Enum):
    Sun = 0
    Mon = 1
    Tue = 2
    Wed = 3
    Thu = 4
    Fri = 5
    Sat = 6
print(Weekday.Sun)    #Weekday.Sun
print(Weekday.Sun.name)  #Sun
print(Weekday.Sun.value)  #0
print(Weekday.__members__)
#OrderedDict([('Sun', ), ('Mon', ), ('Tue', ), ('Wed', ), ('Thu', ), ('Fri', ), ('Sat', )])
for i in Weekday.__members__:         #打印键列表
    print(i)  #Sun
    print(Weekday[i])  #Weekday.Mon
    print(Weekday[i].value)   #1

类名.属性名.name可以取得属性名,类名.属性名.value可以取得属性的值

类名.__members__可以取得字典,再遍历可取得所有键值对。

你可能感兴趣的:(python)