如何修复Python枚举-AttributeError(name)from None错误? [英] How to fix Python Enum - AttributeError(name) from None error?
本文介绍了如何修复Python枚举-AttributeError(name)from None错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
尝试在Python 3.7.3中使用枚举,出现以下错误。已经尝试安装-和卸载-enum34,但仍然无法正常工作。是否在虚拟环境中进行了所有操作(如错误所示)。
Trying to use an enum in Python 3.7.3, getting the following error. Already tried to install - and uninstall - enum34, but it still does not work. Did all the operations in a virtual environment (as the error shows).
还有其他方法可以解决此问题(除了使用另一个枚举实现,如< a href = https://stackoverflow.com/questions/36932/how-can-i-represent-an-enum-in-python>这个问题)?
Is there anything else I can do to fix this (except using another enum implementation as shown in this question)?
#enum import:
from enum import Enum
# enum definition:
class Status(Enum):
on: 1
off: 2
# enum utilisation (another class, same file):
self.status = Status.off
# error:
File "C:\dev\python\test\venv\lib\enum.py", line 349, in __getattr__
AttributeError(name) from None
AttributeError: off
推荐答案
用于定义的正确语法枚举是:
The correct syntax for defining an enum is:
class Status(Enum):
on = 1
off = 2
不在上:1
。
这篇关于如何修复Python枚举-AttributeError(name)from None错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文