首先,我运行了以下代码,运行良好:
class Monster:
def __init__(self):
self._can_do = []
print("created a monster")
super().__init__()
class Race(Monster):
""" all races must derive from this """
def __init__(self):
super().__init__()
print("created a race x")
class Human(Race):
def __init__(self):
super().__init__()
self._can_do.append("Do nothing special !")
print("created a human")
class Elf(Race):
def __init__(self):
super().__init__()
self._can_do.append("Avoid sleep")
print("created an elf")
class Class:
""" all classes must derive from this """
def __init__(self):
super().__init__()
print("created a class x")
class Fighter(Class):
def __init__(self):
super().__init__()
self._can_do.append("Hit hard")
print("created a fighter")
class Wizard(Class):
def __init__(self):
super().__init__()
self._can_do.append("Cast spells")
print("created a wizard")
class Hero(Human, Fighter):
def __init__(self):
x = super()
print(f"super = {x}")
super().__init__()
def speak(self):
for action in self._can_do:
print(f"I can {action} !")
print("creating hero 1 :")
hero1 = Hero()
print("hero 1 human fighter says :")
hero1.speak()
结果是:
creating hero 1 :
created a monster
created a class x
created a fighter
created a race x
created a human
hero 1 human fighter says :
I can Hit hard !
I can Do nothing special ! !
然后我又做了一次,稍微改变了代码,如下所示,因为那是我想去的地方:
(使 Hero 类动态继承而不是静态继承)
import typing
class Monster:
def __init__(self):
self._can_do = []
print("created a monster")
super().__init__()
class Race(Monster):
""" all races must derive from this """
def __init__(self):
super().__init__()
print("created a race x")
class Human(Race):
def __init__(self):
super().__init__()
self._can_do.append("Do nothing special !")
print("created a human")
class Elf(Race):
def __init__(self):
super().__init__()
self._can_do.append("Avoid sleep")
print("created an elf")
class Class:
""" all classes must derive from this """
def __init__(self):
super().__init__()
print("created a class x")
class Fighter(Class):
def __init__(self):
super().__init__()
self._can_do.append("Hit hard")
print("created a fighter")
class Wizard(Class):
def __init__(self):
super().__init__()
self._can_do.append("Cast spells")
print("created a wizard")
RaceT = typing.TypeVar('RaceT', bound=Race)
ClassT = typing.TypeVar('ClassT', bound=Class)
class Hero(typing.Generic[RaceT, ClassT]):
def __init__(self):
super().__init__()
def speak(self):
for action in self._can_do:
print(f"I can {action} !")
print("creating hero 1 :")
hero1 = Hero[Human,Fighter]()
print("hero 1 human fighter says :")
hero1.speak()
这一次,一切都出错了:
creating hero 1 :
hero 1 human fighter says :
Traceback (most recent call last):
File "./test2.py", line 61, in <module>
hero1.speak()
File "./test2.py", line 54, in speak
for action in self._can_do:
AttributeError: 'Hero' object has no attribute '_can_do'
似乎使用泛型类创建使得 super 无法找到父类初始值设定项,非?
我错过了什么吗?
最佳答案
这不是通用类型的意思。当你声明
class Hero(typing.Generic[RaceT, ClassT]):
...
那么这意味着 Hero
有两个类型参数。它不意味着 Hero[Human, Fighter]
是 Human
或 Fighter
的子类,或者List[int]
是 int
的子类。通用类型不是动态调整类的父类(super class)的方法。
关于 python 3.6 : Either I miss something either generic typing breaks super chaining for inheritance,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51790229/