为什么会覆盖_generate_next_value_
仅在最后继承的枚举中完成才有效?
例如:
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}')
Output:
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 时如何不会再次重置它?
我想我遗漏了一些关于如何/为什么这样工作的内容。