我有一個Python枚舉這樣的:如何在枚舉類型中定義枚舉成員的映射?
from enum import Enum, unique
@unique
class Levels(Enum):
Unknown = 0
Warning = 1
Error = 2
def __eq__(self, other): return self.value == other.value
def __ne__(self, other): return self.value != other.value
def __lt__(self, other): return self.value < other.value
def __le__(self, other): return self.value <= other.value
def __gt__(self, other): return self.value > other.value
def __ge__(self, other): return self.value >= other.value
__MAPPPING__ = {
"warn": Warning,
"error": Error
}
@classmethod
def Parse(cls, value, default=None):
return cls.__MAPPPING__.get(value, default)
在本例中,我已經提取出的映射到一個類的成員(根據timeit.timeit()
,其更快)。
我現在的問題是,字典值是使用原始的枚舉成員值(整數),而不是創建的枚舉成員(EnumMeta)。這是合理的,因爲當dict被構造時,它不存在。
如何/在哪裏可以鉤入Enum類/ EnumMeta類或我自己的Levels類,以便使用創建的枚舉成員修補字典?
我改進了一點你的解決方案,因爲,預期的枚舉成員是字典中的值。因此,如果在映射中定義了每個__init__調用,則會掃描該詞典並用枚舉成員替換值(int)。 – Paebbels
@帕培爾:啊,我誤解了那部分。簡化你的代碼並更新裝飾器。 –
@Paebbels:如果這僅僅是一個性能增強,爲什麼你不把所有的成員?而且,在3.5中,大部分(如果不是全部)性能命中被刪除。 –