User32999999
User32999999

Reputation: 11

Why is my super().__init__() says i call with too many arguments?

I have the following code:

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)

Output:

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

I have no idea why it says i gave 3 positional arguments here. I clearly in class C(B) call super().init with 2 arguments: self and Templates.

Upvotes: -1

Views: 626

Answers (1)

ShadowRanger
ShadowRanger

Reputation: 155333

Calling instance methods on super() is like calling them on the instance itself; self is passed implicitly. When you pass it explicitly, it ends up getting passed twice (as if you wrote A.__init__(self, self, Templates)). So just use:

super().__init__(Templates)

without passing self explicitly.

Upvotes: 2

Related Questions