使用Python 3+多继承中的super来创建带参数的父类

时间:2018-05-22 08:46:10

标签: python super

我知道super是如何工作的,它通过MRO从上一个基类中找到方法。
因此,如果我有两个基类AB,并让class C继承它们,我应该如何使用super初始化A和{{1}如果他们需要参数?像这样:

B

如果我在每个class A: def __init__(self, a): self.a = a class B: def __init__(self, b): self.b = b class C(A, B): # inherit from A and B def __init__(self, c): A.__init__(self, a=1) B.__init__(self, b=2) # How to use super here? self.c = c 中使用super,我需要确保正确的继承顺序:

class

但是这会使class A: def __init__(self, a): super().__init__(b=3) self.a = a class B: def __init__(self, b): super().__init__() self.b = b class C(A, B): # Here must be (A, B) def __init__(self, c, a): super().__init__(a=a) self.c = c A耦合,这真的很糟糕。我该如何处理这种情况,即B初始化B?不要使用C?什么是最恐怖的方式?

0 个答案:

没有答案