欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

关于python的枚举代码实例

程序员文章站 2023-09-29 17:30:35
关于python的枚举代码实例 #coding=utf-8 from enum import Enum,unique @unique class Weekday(En...

关于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__可以取得字典,再遍历可取得所有键值对。