【发布时间】:2021-03-29 09:54:03
【问题描述】:
为什么覆盖 _generate_next_value_ 仅在 LAST 继承的枚举中完成?
例如:
class AutoEnum(Enum):
def _generate_next_value_(name, start, count, last_values):
return 'overriding _generate_next_value'
class OtherEnum(Enum):
def some_other_method(self):
return
class AutoOther(AutoEnum, OtherEnum):
TEST = auto()
class OtherAuto(OtherEnum, AutoEnum):
TEST = auto()
print(f'{AutoOther.TEST}: mro={getmro(AutoOther)}\n'
f'name: {AutoOther.TEST.name}, value: {AutoOther.TEST.value}')
print(f'{OtherAuto.TEST}: mro={getmro(OtherAuto)}\n'
f'name: {OtherAuto.TEST.name}, value: {OtherAuto.TEST.value}')
输出:
AutoOther.TEST: mro=(<enum 'AutoOther'>, <enum 'AutoEnum'>, <enum 'OtherEnum'>, <enum 'Enum'>, <class 'object'>)
name: TEST, value: 1
OtherAuto.TEST: mro=(<enum 'OtherAuto'>, <enum 'OtherEnum'>, <enum 'AutoEnum'>, <enum 'Enum'>, <class 'object'>)
name: TEST, value: overriding _generate_next_value
如果每次都设置默认的_generate_next_value_,如果它没有被特别覆盖,那么在继承 Enum 时它如何不被再次重置?
我想我错过了一些关于如何/为什么这样工作的东西。
【问题讨论】:
-
在自动其他中覆盖它?
-
我不是在问如何在此处更改功能,如果不清楚的话。我在问为什么会有这样的行为。
标签: python python-3.x enums multiple-inheritance