枚舉類型可以看作是一種標(biāo)簽或是一系列常量的集合,通常用于表示某些特定的有限集合鞍盗,例如星期般甲、月份、狀態(tài)等欣除。Python 的原生類型(Built-in types)里并沒有專門的枚舉類型挪略,但是我們可以通過很多方法來實現(xiàn)它,例如字典挽牢、類等:
WEEKDAY = {
'MON': 1,
'TUS': 2,
'WEN': 3,
'THU': 4,
'FRI': 5
}
class Color:
RED = 0
GREEN = 1
BLUE = 2
上面兩種方法可以看做是簡單的枚舉類型的實現(xiàn)摊求,如果只在局部范圍內(nèi)用到這樣的枚舉變量是沒有問題的,但問題在于它們都是可變的(mutable)睹栖,也就是說可以在其它地方被修改茧痕,從而影響其正常使用:
WEEKDAY['MON'] = WEEKDAY['FRI']
print(WEEKDAY) # {'FRI': 5, 'TUS': 2, 'MON': 5, 'WEN': 3, 'THU': 4}
通過類定義的枚舉甚至可以實例化,變得不倫不類:
c = Color()
print(c.RED) # 0
Color.RED = 2
print(c.RED) # 2
當(dāng)然也可以使用不可變類型(immutable)曼氛,例如元組,但是這樣就失去了枚舉類型的本意徽级,將標(biāo)簽退化為無意義的變量:
COLOR = ('R', 'G', 'B')
print(COLOR[0], COLOR[1], COLOR[2]) # R G B
為了提供更好的解決方案聊浅,Python 通過 PEP 435 在 3.4 版本中添加了 enum 標(biāo)準(zhǔn)庫,3.4 之前的版本也可以通過 pip install enum
下載兼容支持的庫弹澎。enum
提供了 Enum
/IntEnum
/unique
三個工具努咐,用法也非常簡單,可以通過繼承 Enum
/IntEnum
定義枚舉類型佩迟,其中 IntEnum
限定枚舉成員必須為(或可以轉(zhuǎn)化為)整數(shù)類型竿屹,而 unique
方法可以作為修飾器限定枚舉成員的值不可重復(fù)。
創(chuàng)建枚舉
通過子類化 enum 類來定義枚舉拱燃,代碼如下:
import enum
class BugStatus(enum.Enum):
new = 7
incomplete = 6
invalid = 5
wont_fix = 4
in_progress = 3
fix_committed = 2
fix_released = 1
print('\nMember name: {}'.format(BugStatus.wont_fix.name))
print('Member value: {}'.format(BugStatus.wont_fix.value))
# output
# Member name: wont_fix
# Member value: 4
在解析 Enum 類時碗誉,會將每個成員轉(zhuǎn)換成實例,每個實例都有 name 和 value 屬性哮缺,分別對應(yīng)成員的名稱和值尝苇。
迭代枚舉
直接看代碼:
import enum
class BugStatus(enum.Enum):
new = 7
incomplete = 6
invalid = 5
wont_fix = 4
in_progress = 3
fix_committed = 2
fix_released = 1
for status in BugStatus:
print('{:15} = {}'.format(status.name, status.value))
# output
# new = 7
# incomplete = 6
# invalid = 5
# wont_fix = 4
# in_progress = 3
# fix_committed = 2
# fix_released = 1
成員按照在類中的定義順序生成。
比較枚舉
由于枚舉成員未被排序淳玩,因此它們僅支持通過 is
和 ==
進行比較非竿。
import enum
class BugStatus(enum.Enum):
new = 7
incomplete = 6
invalid = 5
wont_fix = 4
in_progress = 3
fix_committed = 2
fix_released = 1
actual_state = BugStatus.wont_fix
desired_state = BugStatus.fix_released
print('Equality:',
actual_state == desired_state,
actual_state == BugStatus.wont_fix)
print('Identity:',
actual_state is desired_state,
actual_state is BugStatus.wont_fix)
print('Ordered by value:')
try:
print('\n'.join(' ' + s.name for s in sorted(BugStatus)))
except TypeError as err:
print(' Cannot sort: {}'.format(err))
# output
# Equality: False True
# Identity: False True
# Ordered by value:
# Cannot sort: '<' not supported between instances of 'BugStatus' and 'BugStatus'
大小比較引發(fā) TypeError
異常。
繼承 IntEnum
類創(chuàng)建的枚舉類侮东,成員間支持大小比較,代碼如下:
import enum
class BugStatus(enum.IntEnum):
new = 7
incomplete = 6
invalid = 5
wont_fix = 4
in_progress = 3
fix_committed = 2
fix_released = 1
print('Ordered by value:')
print('\n'.join(' ' + s.name for s in sorted(BugStatus)))
# output
# Ordered by value:
# fix_released
# fix_committed
# in_progress
# wont_fix
# invalid
# incomplete
# new
唯一枚舉值
具有相同值的枚舉成員將作為對同一成員對象的別名引用驱敲,在迭代過程中宽闲,不會被打印出來。
import enum
class BugStatus(enum.Enum):
new = 7
incomplete = 6
invalid = 5
wont_fix = 4
in_progress = 3
fix_committed = 2
fix_released = 1
by_design = 4
closed = 1
for status in BugStatus:
print('{:15} = {}'.format(status.name, status.value))
print('\nSame: by_design is wont_fix: ',
BugStatus.by_design is BugStatus.wont_fix)
print('Same: closed is fix_released: ',
BugStatus.closed is BugStatus.fix_released)
# output
# new = 7
# incomplete = 6
# invalid = 5
# wont_fix = 4
# in_progress = 3
# fix_committed = 2
# fix_released = 1
#
# Same: by_design is wont_fix: True
# Same: closed is fix_released: True
因為 by_design 和 closed 是其他成員的別名娩梨,所以沒有被打印览徒。在枚舉中,第一個出現(xiàn)的值是有效的纽什。
如果想讓每一個成員都有唯一值躲叼,可以使用 @unique
裝飾器。
import enum
@enum.unique
class BugStatus(enum.Enum):
new = 7
incomplete = 6
invalid = 5
wont_fix = 4
in_progress = 3
fix_committed = 2
fix_released = 1
# This will trigger an error with unique applied.
by_design = 4
closed = 1
# output
# Traceback (most recent call last):
# File "enum_unique_enforce.py", line 11, in <module>
# class BugStatus(enum.Enum):
# File ".../lib/python3.6/enum.py", line 834, in unique
# (enumeration, alias_details))
# ValueError: duplicate values found in <enum 'BugStatus'>:
# by_design -> wont_fix, closed -> fix_released
如果成員中有重復(fù)值让蕾,會有 ValueError
的報錯或听。
以編程方式創(chuàng)建枚舉
在一些情況下,通過編程的方式創(chuàng)建枚舉,比直接在類中硬編碼更方便萌抵。如果采用這種方式,還可以傳遞成員的 name 和 value 到類的構(gòu)造函數(shù)霎桅。
import enum
BugStatus = enum.Enum(
value='BugStatus',
names=('fix_released fix_committed in_progress '
'wont_fix invalid incomplete new'),
)
print('Member: {}'.format(BugStatus.new))
print('\nAll members:')
for status in BugStatus:
print('{:15} = {}'.format(status.name, status.value))
# output
# Member: BugStatus.new
#
# All members:
# fix_released = 1
# fix_committed = 2
# in_progress = 3
# wont_fix = 4
# invalid = 5
# incomplete = 6
# new = 7
參數(shù) value
代表枚舉的名稱讨永,names
表示成員卿闹。如果給 name 傳遞的參數(shù)是字符串萝快,那么會對這個字符串從空格和逗號處進行拆分著角,將拆分后的單個字符串作為成員的名稱,然后再對其賦值吏口,從 1 開始产徊,以此類推。
為了更好地控制與成員關(guān)聯(lián)的值舟铜, names
可以使用元組或?qū)⒚Q映射到值的字典替換字符串。什么意思奕谭,看下面的代碼:
import enum
BugStatus = enum.Enum(
value='BugStatus',
names=[
('new', 7),
('incomplete', 6),
('invalid', 5),
('wont_fix', 4),
('in_progress', 3),
('fix_committed', 2),
('fix_released', 1),
],
)
print('All members:')
for status in BugStatus:
print('{:15} = {}'.format(status.name, status.value))
# output
# All members:
# new = 7
# incomplete = 6
# invalid = 5
# wont_fix = 4
# in_progress = 3
# fix_committed = 2
# fix_released = 1
在這里例子中痴荐,names
是一個列表,列表中的元素是元組难捌。
非整數(shù)成員值
枚舉成員值不限于整數(shù)鸦难。實際上,任何類型的對象都可以作為枚舉值击敌。如果值是元組拴事,則成員將作為單獨的參數(shù)傳遞給__init__()
。
import enum
class BugStatus(enum.Enum):
new = (7, ['incomplete', 'invalid', 'wont_fix', 'in_progress'])
incomplete = (6, ['new', 'wont_fix'])
invalid = (5, ['new'])
wont_fix = (4, ['new'])
in_progress = (3, ['new', 'fix_committed'])
fix_committed = (2, ['in_progress', 'fix_released'])
fix_released = (1, ['new'])
def __init__(self, num, transitions):
self.num = num
self.transitions = transitions
def can_transition(self, new_state):
return new_state.name in self.transitions
print('Name:', BugStatus.in_progress)
print('Value:', BugStatus.in_progress.value)
print('Custom attribute:', BugStatus.in_progress.transitions)
print('Using attribute:', BugStatus.in_progress.can_transition(BugStatus.new))
# output
# Name: BugStatus.in_progress
# Value: (3, ['new', 'fix_committed'])
# Custom attribute: ['new', 'fix_committed']
# Using attribute: True
在此示例中衡瓶,每個成員值是一個元組牲证,其中包含數(shù)字和列表。
對于更復(fù)雜的情況十厢,元組可能就不那么方便了。由于成員值可以是任何類型的對象澈驼,因此如果有大量需要鍵值對數(shù)據(jù)結(jié)構(gòu)的枚舉值場景筛武,字典就派上用場了。
import enum
class BugStatus(enum.Enum):
new = {
'num': 7,
'transitions': [
'incomplete',
'invalid',
'wont_fix',
'in_progress',
],
}
incomplete = {
'num': 6,
'transitions': ['new', 'wont_fix'],
}
invalid = {
'num': 5,
'transitions': ['new'],
}
wont_fix = {
'num': 4,
'transitions': ['new'],
}
in_progress = {
'num': 3,
'transitions': ['new', 'fix_committed'],
}
fix_committed = {
'num': 2,
'transitions': ['in_progress', 'fix_released'],
}
fix_released = {
'num': 1,
'transitions': ['new'],
}
def __init__(self, vals):
self.num = vals['num']
self.transitions = vals['transitions']
def can_transition(self, new_state):
return new_state.name in self.transitions
print('Name:', BugStatus.in_progress)
print('Value:', BugStatus.in_progress.value)
print('Custom attribute:', BugStatus.in_progress.transitions)
print('Using attribute:', BugStatus.in_progress.can_transition(BugStatus.new))
# output
# Name: BugStatus.in_progress
# Value: (3, ['new', 'fix_committed'])
# Custom attribute: ['new', 'fix_committed']
# Using attribute: True
這個例子和上面用元組是等價的内边。
相關(guān)文檔: