【发布时间】:2020-07-24 19:20:54
【问题描述】:
我遇到了一个我似乎无法弄清楚的多重继承问题。这是一个非常抽象的最小示例,它重现了我的错误(我的代码比这复杂得多)。
class Thing(object):
def __init__(self, x=None):
self.x = x
class Mixin(object):
def __init__(self):
self.numbers = [1,2,3]
def children(self):
return [super().__init__(x=num) for num in self.numbers]
class CompositeThing(Mixin, Thing):
def __init__(self):
super().__init__()
def test(self):
for child in self.children():
print(child.x)
obj = CompositeThing()
obj.test()
根据this,我希望children() 方法返回从self.numbers 构建的Things 列表。相反,我得到TypeError: super(type, obj): obj must be an instance or subtype of type。顺便说一句,如果我不调用构造函数并允许孩子返回super() 3 次(即未实例化的超类),也会发生同样的事情。任何想法为什么会发生这种情况?
提前致谢!
【问题讨论】:
标签: python-3.x multiple-inheritance mixins super