0

Assume I have two classes

class A:
    def __init__(self, v1, v2):
        self.v1 = v1
        self.v2 = v2

class B:
    def __init__(self):
        self.v = 'something'

and a subclass that inherits both and needs to call both constructors (that have different arguments):

class C(A, B):
    def __init__(self, v1, v2):
        # Is the following correct?
        A.__init__(self, v1, v2)
        B.__init__(self)

Is the above the correct way to do that? Or is there a way to exploit super() even when the __init__ arguments of the superclasses do not match?

Federico Taschin
  • 2,027
  • 3
  • 15
  • 28
  • 1
    I believe this is why the recommendation is for all constructors to accept arbitrary keyword arguments. – D. SM Aug 16 '20 at 19:59
  • I don't fully understand how that would solve this issue. Could you please provide me and example? Thank you very much – Federico Taschin Aug 16 '20 at 20:25
  • 1
    Change both constructors to take **kwargs and retrieve individual arguments from kwargs. – D. SM Aug 16 '20 at 21:54
  • 1
    See https://stackoverflow.com/questions/8972866/correct-way-to-use-super-argument-passing. – D. SM Aug 16 '20 at 21:55

0 Answers0