在python中繼承類中調用構造函數的正確方法是什麼?構造函數在繼承類python中失敗
例如,
class A:
def __init__(self, x, y):
self.x = x
self.y = y
class B(A):
def __init__(self, x, y, z):
self.z = z
# super().__init__(x, y) # <== works
super(x, y) # <== throws TypeError: must be type, not int
a = A(2, 3)
b = B(2, 3, 4)
是否有可能調用構造不從基類調用__init __()方法?
您是否仔細閱讀過'super()'文檔? –