python基础-枚举定义错误码

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# author:SunXiuWen
# datetime:2019/11/18 0018 14:04

from enum import Enum, unique

"""经验证和文档发现仅仅用于py3,py2无法使用"""


@unique
class RechargeError(Enum):
    """
    仅仅python3版本可以使用
    """
    CODE_0 = {'0': '成功'}

    @property
    def get_code(self):
        """
        根据枚举名称取状态码code
        :return:状态码code
        """
        return list(self.value.keys())[0]

    @property
    def get_msg(self):
        """
        根据枚举名称取状态说明message
        :return:
        """
        return list(self.value.values())[0]

    def format_msg(self, *args):
        """
        根据枚举名称取状态说明message + 原说明上增加说明文字
        :return:
        """
        return list(self.value.values())[0] + "".join(args)


print(RechargeError.CODE_0.name)
print(RechargeError.CODE_0.value)

print(RechargeError.CODE_0.get_code)
print(RechargeError.CODE_0.get_msg)

人生苦短,我用python!
原文地址:https://www.cnblogs.com/sunxiuwen/p/14475709.html