在 Python 3.4 中扩展 Enum
类型的最佳实践是什么?甚至有可能这样做吗?
例如:
from enum import Enum
class EventStatus(Enum):
success = 0
failure = 1
class BookingStatus(EventStatus):
duplicate = 2
unknown = 3
Traceback (most recent call last):
...
TypeError: Cannot extend enumerations
目前没有可能的方法来创建具有成员的基本枚举类并将其用于其他枚举类(如上面的示例中)。有没有其他方法可以实现 Python 枚举的继承?
最佳答案
Subclassing an enumeration is allowed only if the enumeration does not define any members.
Allowing subclassing of enums that define members would lead to a violation of some important invariants of types and instances.
https://docs.python.org/3/library/enum.html#restricted-enum-subclassing
所以不,这不是直接可能的。
关于python - 如何扩展 Python 枚举?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33679930/