各位大佬,求救一个 Python 问题,关于 Enum 的使用 - V2EX
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
yanyuechuixue
V2EX    Python

各位大佬,求救一个 Python 问题,关于 Enum 的使用

  •  
  •   yanyuechuixue 2024-11-24 22:48:37 +08:00 1754 次点击
    这是一个创建于 385 天前的主题,其中的信息可能已经有所发展或是发生改变。

    我写了一个程序,其中有一段是这样的:

    class Detector(Enum): BBO = "BBO" DECIGO = "DECIGO" @property def ell_max(self) -> int: ell_max = { Detector.BBO: 30, Detector.DECIGO: 30, } return ell_max[self] 

    我现在需要在一个循环中改变 ell_max 的值,也就是不设置为 30 了,而是设置成其他值。我尝试过定义一个 def set_ell_max(self, ell_max) 但最终失败了。我的失败品如下:

    class Detector(Enum): BBO = "BBO" DECIGO = "DECIGO" _ell_max_values = { DECIGO: 30, BBO: 30, } @property def ell_max(self) -> int: return self._ell_max_values[self] def set_ell_max(self, value: int): self._ell_max_values[self] = value 

    这种情况下,当我使用 Detector.BBO.ell_max 的时候,会提示 ```TypeError: 'Detector' object is not subscriptable

     请问各位大哥,我该怎么处理呀?
    第 1 条附言    2024-11-25 10:23:29 +08:00
    感谢 @ma46 大佬
    问题解决!

    是因为定义的字典被转换成枚举了,需要定义成私有的才不会被转换成枚举
    laonger
        1
    laonger  
       2024-11-25 07:09:13 +08:00
    self._ell_max_values[self.name]
    laonger
        2
    laonger  
       2024-11-25 07:09:33 +08:00   1
    self._ell_max_values[self.name]

    试试?
    yanyuechuixue
        3
    yanyuechuixue  
    OP
       2024-11-25 08:39:27 +08:00
    @laonger 还是不行。。

    ``` python
    58 @property
    59 def ell_max(self) -> int:
    ---> 60 return self._ell_max_values[self.name]

    TypeError: 'Detector' object is not subscriptable
    ```

    我想通过类继承的办法定义一个新的类,结果也不好使,说 Enum 的不允许再次继承。。。
    lyxxxh2
        4
    lyxxxh2  
       2024-11-25 09:31:09 +08:00   1
    虽然我没用过 Python 的 Enum, 但是找 self 下标??? 第一次见。
    ```
    def set_ell_max(self, value: int):
    self._ell_max_values[DECIGO] = value
    return self
    ```
    ma46
        5
    ma46  
       2024-11-25 09:58:49 +08:00   1
    私有名称才不会转为枚举类型, 所以变量名要用双下划线

    class Detector(Enum):
    BBO = "BBO"
    DECIGO = "DECIGO"

    __ell_max_values = {
    DECIGO: 30,
    BBO: 30,
    }

    @property
    def ell_max(self) -> int:
    print(self.__ell_max_values)
    return self.__ell_max_values[self.value]

    def set_ell_max(self, value: int):
    self.__ell_max_values[self.value] = value
    yanyuechuixue
        6
    yanyuechuixue  
    OP
       2024-11-25 10:22:35 +08:00
    @ma46 谢谢!学到了,原来如此!

    十分感谢大佬!!

    问题解决!
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     5188 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 27ms UTC 08:28 PVG 16:28 LAX 00:28 JFK 03:28
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86