考虑以下代码:
from enum import Enum
class SubclassOfEnum(Enum):
x = 5
print(SubclassOfEnum.x)
class SubSubclassOfEnum(SubclassOfEnum):
y = 6
print(SubSubclassOfEnum.y)
我们得到一个错误,TypeError: Cannot extend enumerations
,
来自:Python36\lib\enum.py",第 436 行,在 _get_mixins_ 中
最佳答案
因为用成员子类化 Enum
是 specifically disallowed .
对于 Enum
的一般用例,请查看 When and where to use... .
对于 extending Enum
s (将成员添加到现有的 Enum
,而不是子类化它们)...
关于python - 为什么我不能继承 Enum 的子类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49806171/