【发布时间】:2018-12-03 14:48:57
【问题描述】:
我有一种情况,我必须初始化所有的基类
class B:
def __init__(self):
print("B.__init__")
class C:
def __init__(self):
print("C.__init__")
class D(B,C):
def __init__(self):
print("D.__init__")
super().__init__()
class E(D):
def __init__(self):
print("E.__init__")
super().__init__()
x = E()
但是上面的代码导致
E.__init__
D.__init__
B.__init__
我关心的是为什么 C 没有被初始化?
【问题讨论】:
-
你希望
super调用这两个构造函数吗? -
因为
B在方法解析顺序中位于C之前,但不包含对super的调用 -
super比看起来要复杂一些。 Here's a good article 由一位 Python 开发人员讲述其用途。
标签: python multiple-inheritance