iT邦幫忙

2018 iT 邦幫忙鐵人賽
0

概觀

python3.4之後引進的功能。若要在python2.7使用aenum

enumeration由eunm member組成,每個memeber包含namevalue

from enum import Enum


class Model(Enum):
    IRON = 'iron man'
    FLASH = 'flash man'
    SPIDER = 'spider man'


print('enum member:{}'.format(Model.IRON))
print('name of enum member:{}'.format(Model.IRON.name))
print('value of enum member:{}'.format(Model.IRON.value))
print('type of enum member:{}'.format(type(Model.IRON)))
print('type of enum member name:{}'.format(type(Model.IRON.name)))
print('type of enum member value:{}'.format(type(Model.IRON.value)))

# enum member:Model.IRON
# name of enum member:IRON
# value of enum member:iron man
# type of enum member:<enum 'Model'>
# type of enum member name:<class 'str'>
# type of enum member value:<class 'str'>


def welcome_controller(request_type):
    welcome_msg = 'Hi, I\'m {}'
    print(welcome_msg.format(request_type.value))

welcome_controller(Model.IRON)
try:
    welcome_controller(Model('flash man'))
    welcome_controller(Model('super man'))
except ValueError as e:
    print(e)
    
# Hi, I'm iron man
# Hi, I'm flash man
# 'super man' is not a valid Model

快速宣告,value會自動使用int

from enum import Enum


Model = Enum('Model', 'IRON SPIDER FLASH SUPER')
for m in Model:
    print('Model:{}, name:{}, value:{}'.format(m, m.name, m.value))
    print('type of value:{}'.format(type(m.value)))

# Model:Model.IRON, name:IRON, value:1
# type of value:<class 'int'>
# Model:Model.SPIDER, name:SPIDER, value:2
# type of value:<class 'int'>
# Model:Model.FLASH, name:FLASH, value:3
# type of value:<class 'int'>
# Model:Model.SUPER, name:SUPER, value:4
# type of value:<class 'int'>

eunm member

name不可重複,value可以重複,亦即有別名的概念。若要限制value不能重複,可以使用decorator @unique

from enum import Enum


class ResponseTypeDuplicateName(Enum):
    SUCCESS = 0
    LICENSE_NOT_VALID = 100
    LICENSE_EXPIRED = 101
    INPUT_DATA_INVALID = 200
    INPUT_DATA_INVALID = 201


print(ResponseTypeDuplicateName.INPUT_DATA_INVALID)

# TypeError: Attempted to reuse key: 'INPUT_DATA_INVALID'
from enum import Enum, unique


@unique
class ResponseTypeUniqueName(Enum):
    SUCCESS = 0
    LICENSE_NOT_VALID = 100
    LICENSE_EXPIRED = 101
    INPUT_DATA_INVALID = 200
    JSON_DATA_INVALID = 200

# ValueError: duplicate values found in <enum 'ResponseTypeUniqueName'>: JSON_DATA_INVALID -> INPUT_DATA_INVALID

iteration

支援iteration,結果不包含別名,若結果要包含別名可以使用__members__

from enum import Enum, unique


class ResponseTypeAlias(Enum):
    SUCCESS = 0
    LICENSE_NOT_VALID = 100
    LICENSE_EXPIRED = 101
    INPUT_DATA_INVALID = 200
    JSON_DATA_INVALID = 200


print(ResponseTypeAlias.INPUT_DATA_INVALID)
print(ResponseTypeAlias.JSON_DATA_INVALID)

# ResponseTypeAlias.INPUT_DATA_INVALID
# ResponseTypeAlias.INPUT_DATA_INVALID


for r in ResponseTypeAlias:
    print(r)

# ResponseTypeAlias.SUCCESS
# ResponseTypeAlias.LICENSE_NOT_VALID
# ResponseTypeAlias.LICENSE_EXPIRED
# ResponseTypeAlias.INPUT_DATA_INVALID


for name, member in ResponseTypeAlias.__members__.items():
    print('name:{}, member:{}'.format(name, member))

# name:SUCCESS, member:ResponseTypeAlias.SUCCESS
# name:LICENSE_NOT_VALID, member:ResponseTypeAlias.LICENSE_NOT_VALID
# name:LICENSE_EXPIRED, member:ResponseTypeAlias.LICENSE_EXPIRED
# name:INPUT_DATA_INVALID, member:ResponseTypeAlias.INPUT_DATA_INVALID
# name:JSON_DATA_INVALID, member:ResponseTypeAlias.INPUT_DATA_INVALID

參考


上一篇
Day27-datetime-module
下一篇
Day29-if-成立或不成立
系列文
if len(learning.python) == 30:31
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言