简体   繁体   English

为什么我不能继承Enum的子类?

[英]Why can't I subclass a subclass of Enum?

Consider the following code: 考虑以下代码:

from  enum import Enum

class SubclassOfEnum(Enum):
    x = 5
print(SubclassOfEnum.x)

class SubSubclassOfEnum(SubclassOfEnum):
    y = 6
print(SubSubclassOfEnum.y)

We get an error, TypeError: Cannot extend enumerations , 我们收到一个错误, TypeError: Cannot extend enumerations

from: Python36\\lib\\enum.py", line 436, in _get_mixins_ 来自: Python36\\lib\\enum.py", line 436, in _get_mixins_

Because subclassing Enum s with members is specifically disallowed . 因为明确禁止使用成员对Enum进行子类化。

For general use-cases for Enum check out When and where to use... . 有关Enum一般用例,请查看何时何地使用...。

For extending Enum s (adding members to existing Enum s, not subclassing them)... 用于扩展Enum (将成员添加到现有Enum ,而不是对其进行子类化)...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM