python基础——枚举类
python基础——枚举类
当我们需要定义常量时,一个办法是用大写变量通过整数来定义,例如月份:
JAN = 1 FEB = 2 MAR = 3 ... NOV = 11 DEC = 12
好处是简单,缺点是类型是int
,并且仍然是变量。
更好的方法是为这样的枚举类型定义一个class类型,然后,每个常量都是class的一个唯一实例。Python提供了Enum
类来实现这个功能:
from enum import Enum Month = Enum('Month', ('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'))
这样我们就获得了Month
类型的枚举类,可以直接使用Month.Jan
来引用一个常量,或者枚举它的所有成员:
for name, member in Month.__members__.items(): print(name, '=>', member, ',', member.value)
value
属性则是自动赋给成员的int
常量,默认从1
开始计数。
如果需要更精确地控制枚举类型,可以从Enum
派生出自定义类:
from enum import Enum, unique @unique class Weekday(Enum): Sun = 0 # Sun的value被设定为0 Mon = 1 Tue = 2 Wed = 3 Thu = 4 Fri = 5 Sat = 6
@unique
装饰器可以帮助我们检查保证没有重复值。
访问这些枚举类型可以有若干种方法:
>>> day1 = Weekday.Mon >>> print(day1) Weekday.Mon >>> print(Weekday.Tue) Weekday.Tue >>> print(Weekday['Tue']) Weekday.Tue >>> print(Weekday.Tue.value) 2 >>> print(day1 == Weekday.Mon) True >>> print(day1 == Weekday.Tue) False >>> print(Weekday(1)) Weekday.Mon >>> print(day1 == Weekday(1)) True >>> Weekday(7) Traceback (most recent call last): ... ValueError: 7 is not a valid Weekday >>> for name, member in Weekday.__members__.items(): ... print(name, '=>', member) ... Sun => Weekday.Sun Mon => Weekday.Mon Tue => Weekday.Tue Wed => Weekday.Wed Thu => Weekday.Thu Fri => Weekday.Fri Sat => Weekday.Sat
可见,既可以用成员名称引用枚举常量,又可以直接根据value的值获得枚举常量。
小结
Enum
可以把一组相关常量定义在一个class中,且class不可变,而且成员可以直接比较。
参考源码:
#python 枚举类 示例 #2016-8-30 18:55:25 #MengmengCoding # -*- coding: utf-8 -*- from enum import Enum #Enum是一个枚举类 #Month,Weekday各为一个Enum类的实例化 Month=Enum('Month',('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec')) Weekday=Enum('Weekday',('Mon','Tue','Wed','Thu','Fri','Sat','Sun')) for name,member in Month.__members__.items(): print(name,'=>',member,',',member.value) print('--------cut-off line--------') #输出: ''' 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 ''' for name,member in Weekday.__members__.items(): print(name,'=>',member,',',member.value) print('--------cut-off line--------') #输出: ''' Mon => Weekday.Mon , 1 Tue => Weekday.Tue , 2 Wed => Weekday.Wed , 3 Thu => Weekday.Thu , 4 Fri => Weekday.Fri , 5 Sat => Weekday.Sat , 6 Sun => Weekday.Sun , 7 ''' #如果需要更精确地控制枚举类型,可以从Enum派生出自定义类 #这里还是用周来举例吧,谁让我见识少呢~ #用WeekDay来区别之前的Weekday吧 from enum import Enum,unique @unique #@unique装饰器可以帮助我们检查保证没有重复值 class WeekDay(Enum): #由Enum类派生得到 Sun=0 Mon=1 Tue=2 Wed=3 Thu=4 Fri=5 Sat=6 day1=WeekDay.Mon print('day1=',day1) print('WeekDay.Tue=',WeekDay.Tue) print('WeekDay[\'Tue\']=',WeekDay['Tue']) print('WeekDay.Tue.value=',WeekDay.Tue.value) print('day1==WeekDay.Mon ?',day1==WeekDay.Mon) print('day1==WeekDay.Tue ?',day1==WeekDay.Tue) print('day1==WeekDay(1) ?',day1==WeekDay(1)) #输出: ''' day1= WeekDay.Mon WeekDay.Tue= WeekDay.Tue WeekDay['Tue']= WeekDay.Tue WeekDay.Tue.value= 2 day1==WeekDay.Mon ? True day1==WeekDay.Tue ? False day1==WeekDay(1) ? True ''' print('--------cut-off line--------') for name,member in WeekDay.__members__.items(): print(name,'=>',member) #输出: ''' Sun => WeekDay.Sun Mon => WeekDay.Mon Tue => WeekDay.Tue Wed => WeekDay.Wed Thu => WeekDay.Thu Fri => WeekDay.Fri Sat => WeekDay.Sat '''
それでも私の大好きな人