每周一個 Python 模塊 | enum

枚舉類型可以看作是一種標(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)文檔:

https://pymotw.com/3/enum/index.html

http://python.jobbole.com/84112/

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末漠其,一起剝皮案震驚了整個濱河市竿音,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌春瞬,老刑警劉巖宽气,帶你破解...
    沈念sama閱讀 223,126評論 6 520
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異绪氛,居然都是意外死亡涝影,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,421評論 3 400
  • 文/潘曉璐 我一進店門序目,熙熙樓的掌柜王于貴愁眉苦臉地迎上來唆樊,“玉大人刻蟹,你說我怎么就攤上這事。” “怎么了红伦?”我有些...
    開封第一講書人閱讀 169,941評論 0 366
  • 文/不壞的土叔 我叫張陵昙读,是天一觀的道長膨桥。 經(jīng)常有香客問我,道長只嚣,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 60,294評論 1 300
  • 正文 為了忘掉前任蕴掏,我火速辦了婚禮调鲸,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘即供。我一直安慰自己贯钩,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 69,295評論 6 398
  • 文/花漫 我一把揭開白布祸穷。 她就那樣靜靜地躺著勺三,像睡著了一般。 火紅的嫁衣襯著肌膚如雪祈远。 梳的紋絲不亂的頭發(fā)上商源,一...
    開封第一講書人閱讀 52,874評論 1 314
  • 那天,我揣著相機與錄音扫沼,去河邊找鬼。 笑死缎除,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的梢为。 我是一名探鬼主播轰坊,決...
    沈念sama閱讀 41,285評論 3 424
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼袒炉!你這毒婦竟也來了樊零?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 40,249評論 0 277
  • 序言:老撾萬榮一對情侶失蹤夺艰,失蹤者是張志新(化名)和其女友劉穎沉衣,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體豌习,經(jīng)...
    沈念sama閱讀 46,760評論 1 321
  • 正文 獨居荒郊野嶺守林人離奇死亡肥隆,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,840評論 3 343
  • 正文 我和宋清朗相戀三年栋艳,在試婚紗的時候發(fā)現(xiàn)自己被綠了恰聘。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片晴叨。...
    茶點故事閱讀 40,973評論 1 354
  • 序言:一個原本活蹦亂跳的男人離奇死亡矾屯,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出孙技,到底是詐尸還是另有隱情,我是刑警寧澤绪杏,帶...
    沈念sama閱讀 36,631評論 5 351
  • 正文 年R本政府宣布蕾久,位于F島的核電站拌夏,受9級特大地震影響僧著,放射性物質(zhì)發(fā)生泄漏障簿。R本人自食惡果不足惜站故,卻給世界環(huán)境...
    茶點故事閱讀 42,315評論 3 336
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望愈腾。 院中可真熱鬧岂津,春花似錦、人聲如沸吮成。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,797評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽茶宵。三九已至,卻和暖如春节预,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背蛤吓。 一陣腳步聲響...
    開封第一講書人閱讀 33,926評論 1 275
  • 我被黑心中介騙來泰國打工糠赦, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人淌山。 一個月前我還...
    沈念sama閱讀 49,431評論 3 379
  • 正文 我出身青樓,卻偏偏與公主長得像德绿,于是被迫代替她去往敵國和親退渗。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,982評論 2 361

推薦閱讀更多精彩內(nèi)容