Python3 多重继承__init__秒class
Python 3 multiple inheritance __init__ second class
在 Python3 我有一个 class 继承了另外两个 class。
然而,正如我所看到的,当一个对象仅首先被初始化时 class 也看到了示例...
class A:
def __init__(self):
print("A constructor")
class B:
def __init__(self):
print("B constructor")
class C(A, B):
def __init__(self):
print("C constructor")
super().__init__()
c = C()
这有输出:
C constructor
A constructor
我的问题是,为什么它也不调用 B 构造函数?
是否可以使用 super?
从 C class 调用 B 构造函数
class A:
def __init__(self):
print("A constructor")
super().__init__() # We need to explicitly call super's __init__ method. Otherwise, None will be returned and the execution will be stopped here.
class B:
def __init__(self):
print("B constructor")
class C(A, B):
def __init__(self):
print("C constructor")
super().__init__() # This will call class A's __init__ method. Try: print(C.mro()) to know why it will call A's __init__ method and not B's.
c = C()
在 Python3 我有一个 class 继承了另外两个 class。 然而,正如我所看到的,当一个对象仅首先被初始化时 class 也看到了示例...
class A:
def __init__(self):
print("A constructor")
class B:
def __init__(self):
print("B constructor")
class C(A, B):
def __init__(self):
print("C constructor")
super().__init__()
c = C()
这有输出:
C constructor A constructor
我的问题是,为什么它也不调用 B 构造函数? 是否可以使用 super?
从 C class 调用 B 构造函数class A:
def __init__(self):
print("A constructor")
super().__init__() # We need to explicitly call super's __init__ method. Otherwise, None will be returned and the execution will be stopped here.
class B:
def __init__(self):
print("B constructor")
class C(A, B):
def __init__(self):
print("C constructor")
super().__init__() # This will call class A's __init__ method. Try: print(C.mro()) to know why it will call A's __init__ method and not B's.
c = C()