V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
wuwukai007
V2EX  ›  Python

受 django 启发,让枚举类 label 返回中文

  •  
  •   wuwukai007 · 2023-07-19 18:28:03 +08:00 · 1220 次点击
    这是一个创建于 492 天前的主题,其中的信息可能已经有所发展或是发生改变。
    from enum import Enum
    
    class XIntegerChoices(Enum):
        def __new__(cls, value, label):
            obj = object.__new__(cls)
            obj._value_ = value
            obj._label = label
            return obj
    
        def __eq__(self, other):
            if isinstance(other, int):
                return other == self.value
            if isinstance(other, XIntegerChoices):
                return other.value == self.value
            return False
    
        def __ne__(self, other):
            return not self.__eq__(other)
    
        def __lt__(self, other):
            if isinstance(other, int):
                return self.value < other
            if isinstance(other, XIntegerChoices):
                return self.value < other.value
            raise TypeError
    
        def __le__(self, other):
            return self.__eq__(other) or self.__lt__(other)
    
        def __gt__(self, other):
            if isinstance(other, int):
                return self.value > other
            if isinstance(other, XIntegerChoices):
                return self.value > other.value
            raise TypeError
    
        def __ge__(self, other):
            return self.__eq__(other) or self.__gt__(other)
    
        def __hash__(self):
            return hash(self.value)
    
        @property
        def value(self):
            return self._value_
    
        @property
        def label(self):
            return self._label
    
        @classmethod
        def members(self):
            return list(self)
    
    
    class Action(XIntegerChoices):
        CREATED = (1, "创建")
    
    
    if __name__ == '__main__':
    
        print(Action.CREATED == 2)
        print(Action.CREATED.label)
        print(Action.members())
    
    # False
    # 创建
    # [<Action.CREATED: 1>]
    
    4 条回复    2023-07-24 14:26:53 +08:00
    joApioVVx4M4X6Rf
        1
    joApioVVx4M4X6Rf  
       2023-07-20 08:21:56 +08:00
    这个实现方式好棒!
    bthulu
        2
    bthulu  
       2023-07-20 13:36:46 +08:00
    你就不能直接用中文枚举么? 用中文是会要了你的命, 还是丢了你的逼格?
    print(Action.创建)
    wuwukai007
        3
    wuwukai007  
    OP
       2023-07-20 15:22:44 +08:00
    @bthulu 如果是中英文+符号 混搭肯定不行,要考虑下代码健壮性,比如 应用商店(APP)
    gujigujij
        4
    gujigujij  
       2023-07-24 14:26:53 +08:00
    Enum 原生支持的

    ```

    class CardTypeEnum(Enum):
    ID_CARD = (1, '身份证')
    TRAVEL_FOR_HK_MR = (2, '港澳居民来往内地通行证')
    TRAVEL_FOR_TW = (3, '台湾居民来往大陆通行证')
    PASSPORT = (4, '护照')

    def __init__(self, code, text):
    self.code = code
    self.text = text


    print(CardTypeEnum.ID_CARD.text )
    ```
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   2780 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 30ms · UTC 12:52 · PVG 20:52 · LAX 04:52 · JFK 07:52
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.