为什么我的super().__init__()说我调用太多的参数?



我有以下代码:

import numpy as np
Template_Dict = {"x": ["path/to.data", (2, 1),[0,0,0]], "y": ["path/to.data", (3, 3),[-np.pi/2,0,0]],"z": ["path/to.data", (3, 3),[-np.pi/2,0,0]]}
class A:
def __init__(self,Templates):
print("ATemplate",Templates)
print(self)
self.Template = Templates[Prob_Type]
class B(A):
def __init__(self,Templates):
print("BTemplates",Templates)
super().__init__(self, Templates)
class C(B):
def __init__(self,Templates):
print("CTemplate:",Templates)
print(self)
super().__init__(self, Templates)
x=C(Template_Dict)

输出:

CTemplate: {'x': ['path/to.data', (2, 1), [0, 0, 0]], 'y': ['path/to.data', (3, 3), [-1.5707963267948966, 0, 0]], 'z': ['path/to.data', (3, 3), [-1.5707963267948966, 0, 0]]}
<__main__.C object at 0x7f23f1704430>
Traceback (most recent call last):
File "errortest.py", line 22, in <module>
x=C(Template_Dict)
File "errortest.py", line 20, in __init__
super().__init__(self, Templates)
TypeError: __init__() takes 2 positional arguments but 3 were given

我不知道为什么它说我在这里给出了3个位置参数。我清楚地在类C(B)中调用super()init有两个参数:self和Templates。

super()上调用实例方法就像在实例本身上调用它们一样;self隐式传递。当你显式地传递它时,它最终会被传递两次(就像你写的A.__init__(self, self, Templates)一样)。所以使用:

super().__init__(Templates)

不显式传递self

最新更新