[Python] Passing variables to constructors of multiple parent classes in Python using super()...

Discussão em 'Python' iniciado por Stack, Maio 20, 2025.

  1. Stack

    Stack Membro Participativo

    I have a class which depends on multiple parent classes as shown below. Each parent constructor needs its own arguments and I need to pass variables to them:

    class A:
    def __init__(self, var):
    print("A class", var)

    class B:
    def __init__(self, var):
    print("B class", var)

    class C(A, B):
    def __init__(self, var1, var2, var3):
    A.__init__(self, var1)
    B.__init__(self, var2)
    print("C class", var3)


    C("1", "2,", "3")


    Is it possible to make it work using super? I tried the code below, but it tries to pass both variables to A constructor

    class A:
    def __init__(self, var):
    print("A class", var)

    class B:
    def __init__(self, var):
    print("B class", var)

    class C(A, B):
    def __init__(self, var1, var2, var3):
    super(C, self).__init__(var1, var2)
    print("C class", var3)


    C("1", "2,", "3")

    Continue reading...

Compartilhe esta Página