當咱們須要定義常量時,一個方法是用大寫變量經過整數來定義,例如月份python
JAN = 1 FEB = 2 MAR = 3 APR=4 May=5 Jun=6 Jul=7 Aug=8 Sep=9 Oct=10 NOV = 11 DEC = 12
好處是簡單,缺點是類型int,而且仍然是變量。blog
更好的方法是爲這樣的枚舉類型定義一個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來引用一個常量,或者枚舉它全部成員it
>>> Month.Jan <Month.Jan: 1> >>> Month.Feb <Month.Feb: 2>
>>> for name,member in Month.__members__.items(): ... print(name,'=>',member,',',member.value) ... 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
value屬性則是自動賦給成員的int常量,默認從1開始計數ast
若是須要更精確地控制枚舉類型,能夠從Enum派生出自定義類class
from enum import Enum,unique @unique class Weekday(Enum): Sun=0 Mon=1 Tue=2 Wed=3 Thu=4 Fri=5 Sat=6
裝飾器unique保證沒有重複import
訪問這些枚舉值有多種方法變量
>>> 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的值得到枚舉常量。object
練習引用
把Student的gender屬性改成枚舉類型,能夠避免使用字符串
from enum import Enum, unique class Gender(Enum): Male=0 Female=1 class Student(object): def __init__(self, name, gender): self.name = name self.gender = gender bart=Student('Bart',Gender.Male)