1

class A and class B are part of class C, I separate them for the single responsibility principle, so I inherit them in class C, they need different variables for initialization, I faced error when using super().__init__(b=b, d=d)

class A:
    def __init__(self, a):
        print(f"A is init param={a}")


class B:
    def __init__(self, b, d):
        print(f"B is init param={b} {d}")


class C(A, B):
    def __init__(self, a, b, d):
        super().__init__(a=a)
        super().__init__(b=b, d=d)


c = C(a=3, b=5, d=9)

I got error:

super().__init__(b=5, d=9)
TypeError: A.__init__() got an unexpected keyword argument 'b'
A is init param=3

How can I inherit both class A and class B while initialize them with different variables?

Tung
  • 33
  • 3

1 Answers1

0

Unfortunately you can't use super() in this case. You have to call the __init__() methods directly:

class C(A, B):
    def __init__(self, a, b, d):
        A.__init__(self, a=a)
        B.__init__(self, b=b, d=d)
quamrana
  • 37,849
  • 12
  • 53
  • 71