0

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.

  • 2
    Post something that actually runs and reproduces the problem when run. "Kind of" code doesn't cut it if it doesn't reproduce the problem. – user2357112 May 25 '22 at 11:01
  • Just ommit `self` from `super().__init__(self, Templates)`. Calling a method implicitly passes `self` as the first argument. – d-k-bo May 25 '22 at 11:50
  • 1
    Sorry, i changed it and i hope it is more clear whats the problem now :-) – User32999999 May 25 '22 at 11:50

1 Answers1

1

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.

ShadowRanger
  • 143,180
  • 12
  • 188
  • 271