name
,value
两个属性,其中name
为该枚举值的名称,value
为该枚举值的内容;@unique
;__members__
;from enum import Enum from typing import Optional class BaseEnm(Enum): def getCode(self): """根据枚举名称取状态码code @return: 状态码code """ return self.value[0] def getMessage(self): """根据枚举名称取状态说明message @return: 状态说明message """ return self.value[1] @classmethod def messageByCode(cls, code) -> Optional[str]: """根据状态码获取状态码说明""" for name, member in cls.__members__.items(): if code in member.value: return cls[name].getMessage() return None @unique class CallStateResult(BaseEnm): """通话状态结果""" USER_STATE_NULL = (0, "未知的初始状态") USER_STATE_ANSWERED = (1, "接通") USER_STATE_POWER_OFF = (2, "关机") USER_STATE_NOT_EXIST = (3, "空号") USER_STATE_INVALID_NUM = (4, "非法号码") USER_STATE_OUTSERVICE = (5, "停机") USER_STATE_NOT_IN_SERVICE = (6, "不在服务区") USER_STATE_NOT_ANSWER = (7, "无人接听") USER_STATE_BUSY = (8, "用户忙") USER_STATE_NOT_REACHABLE = (9, "无法接通") USER_STATE_NOT_LOCALNUM_NEED_ZERO = (10, "本地号码未加零") USER_STATE_LOCALNUM_ADD_ZERO = (11, "本地号码多加零") USER_STATE_BARRING_INCOMING = (12, "呼入限制") USER_STATE_CALL_REMINDER = (13, "来电提醒") USER_STATE_DEFAULTING = (14, "欠费") USER_STATE_LINE_BUSY = (15, "网络忙") USER_STATE_FORWARDED = (16, "呼叫转移失败") USER_STATE_CANNOT_CONNECTED = (17, "无法接听") USER_STATE_NUMBER_CHANGE = (18, "改号") USER_STATE_LINE_FAULT = (19, "线路故障") USER_STATE_CALL_REJECTED = (20, "拒接") USER_STATE_NOT_RECOGNITION = (21, "未知状态") USER_STATE_CALL_FAILED = (22, "呼叫失败") USER_STATE_HANGUP = (100, "通话结束") if __name__ == '__main__': print(CallStateResult.USER_STATE_NULL) print(CallStateResult.USER_STATE_NULL.getCode()) print(CallStateResult.USER_STATE_NULL.getMessage()) print(CallStateResult.messageByCode(0))
输出
CallStateResult.USER_STATE_NULL 0 未知的初始状态 未知的初始状态