如何修复 Python class 'NameError: name 'enum' is not defined'
问题:
你正在尝试在 Python 中从 enum 继承一个类:
myenum_example.py
class MyEnum(enum):
X = 1
Y = 2但当你尝试运行它时,你看到此错误消息:
nameerror_output.txt
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-15-ebcfa41a8a7c> in <module>
----> 1 class MyEnum(enum):
2 X = 1
3 Y = 2
NameError: name 'enum' is not defined解决方案
你需要从 Enum(大写 E!)继承,而不是从 enum!正确的语法是
myenum_fixed.py
from enum import Enum
class MyEnum(Enum):
X = 1
Y = 2Check out similar posts by category:
Python
If this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow