每週一個 Python 模組 | enum
專欄地址:每週一個 Python 模組
列舉型別可以看作是一種標籤或是一系列常量的集合,通常用於表示某些特定的有限集合,例如星期、月份、狀態等。Python 的原生型別(Built-in types)裡並沒有專門的列舉型別,但是我們可以通過很多方法來實現它,例如字典、類等:
WEEKDAY = {
'MON': 1,
'TUS': 2,
'WEN': 3,
'THU': 4,
'FRI': 5
}
class Color:
RED = 0
GREEN = 1
BLUE = 2
複製程式碼
上面兩種方法可以看做是簡單的列舉型別的實現,如果只在區域性範圍內用到這樣的列舉變數是沒有問題的,但問題在於它們都是可變的(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
複製程式碼
當然也可以使用不可變型別(immutable),例如元組,但是這樣就失去了列舉型別的本意,將標籤退化為無意義的變數:
COLOR = ('R', 'G', 'B')
print(COLOR[0], COLOR[1 ], COLOR[2]) # R G B
複製程式碼
為了提供更好的解決方案,Python 通過 PEP 435 在 3.4 版本中添加了 enum 標準庫,3.4 之前的版本也可以通過 pip install enum
下載相容支援的庫。enum
提供了 Enum
/IntEnum
/unique
三個工具,用法也非常簡單,可以通過繼承 Enum
/IntEnum
定義列舉型別,其中 IntEnum
限定列舉成員必須為(或可以轉化為)整數型別,而 unique
方法可以作為修飾器限定列舉成員的值不可重複。
建立列舉
通過子類化 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 類時,會將每個成員轉換成例項,每個例項都有 name 和 value 屬性,分別對應成員的名稱和值。
迭代列舉
直接看程式碼:
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'
複製程式碼
大小比較引發 TypeError
異常。
繼承 IntEnum
類建立的列舉類,成員間支援大小比較,程式碼如下:
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 是其他成員的別名,所以沒有被列印。在列舉中,第一個出現的值是有效的。
如果想讓每一個成員都有唯一值,可以使用 @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
複製程式碼
如果成員中有重複值,會有 ValueError
的報錯。
以程式設計方式建立列舉
在一些情況下,通過程式設計的方式建立列舉,比直接在類中硬編碼更方便。如果採用這種方式,還可以傳遞成員的 name 和 value 到類的建構函式。
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
複製程式碼
引數 value
代表列舉的名稱,names
表示成員。如果給 name 傳遞的引數是字串,那麼會對這個字串從空格和逗號處進行拆分,將拆分後的單個字串作為成員的名稱,然後再對其賦值,從 1 開始,以此類推。
為了更好地控制與成員關聯的值, names
可以使用元組或將名稱對映到值的字典替換字串。什麼意思,看下面的程式碼:
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
是一個列表,列表中的元素是元組。
非整數成員值
列舉成員值不限於整數。實際上,任何型別的物件都可以作為列舉值。如果值是元組,則成員將作為單獨的引數傳遞給__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
複製程式碼
在此示例中,每個成員值是一個元組,其中包含數字和列表。
對於更復雜的情況,元組可能就不那麼方便了。由於成員值可以是任何型別的物件,因此如果有大量需要鍵值對資料結構的列舉值場景,字典就派上用場了。
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
複製程式碼
這個例子和上面用元組是等價的。
相關文件: