I have the following code:
class A:
def __init__(self):
print("A.__init__")
class B(A):
def __init__(self):
print("B.__init__")
super().__init__()
class C(A):
def __init__(self):
print("C.__init__")
super().__init__()
class D(B,C):
def __init__(self):
print("D.__init__")
super().__init__()
When I instantiate an object out of class D,
d = D()
I got:
>>> d = D()
D.__init__
B.__init__
C.__init__
A.__init__
Why did class A's __init__()
only got called once, and at the very end? Note both class B and class C called class A's __init__()
, so I expected that class A's __init__()
should be called twice.
I read both Python 3's documentation and Mr. Hettinger's blog, but didn't figure out the underlying mechanism. So how does the super()
determine that it only needs to execute once? Thanks.