跳到主要内容

Use super() to Call func() of Its Parent Class

· 阅读需 1 分钟
Benjamin

As is shown below, I wanted to call init() of A,B and C, but found super(A,self).__init__() calling B.__init__() and super(C,self).__init__() calling built-in __init__().

class A:
def __init__(self) -> None:
print("init A")

class B:
def __init__(self) -> None:
print("init B")


class C():
def __init__(self) -> None:
print("init C")

class D(A,B,C):
def __init__(self) -> None:
super(A,self).__init__() # call B.__init__()
super(B,self).__init__() # call C.__init__()
super(C,self).__init__() # call built-in __init__()

d=D()
"""
expected output:
init A
init B
init C

output:
init B
init C
"""

The confusing phenomenon is due to my mistakes on super().super(A,self).func() call the func() of next parent class in MRO not A.func().

Following is the right code.

class A:
def __init__(self) -> None:
print("init A")

class B:
def __init__(self) -> None:
print("init B")

class C():
def __init__(self) -> None:
print("init C")

class D(A,B,C):
def __init__(self) -> None:
super().__init__() # call B.__init__()
super(A,self).__init__() # call C.__init__()
super(B,self).__init__() # call built-in __init__()

d=D()

"""
output:
init A
init B
init C
"""
Loading Comments...