1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

[Python] Why is explicit Parent.__init__(self) discouraged in Python Multiple Inheritance...

Discussão em 'Python' iniciado por Stack, Novembro 25, 2025.

  1. Stack

    Stack Membro Participativo

    Body: I am learning Multiple Inheritance in Python. In my code, I am initializing the parent classes by calling them explicitly by name, like this:

    I read that I should use super(), but when I use super(), it only seems to call the first parent (Parent1).


    1. Does explicit calling (Parent1.__init__) break the Method Resolution Order (MRO)?


    2. How do I correctly use super() to initialize both parents in Multiple Inheritance?

    [class Parent1:
    def __init__(self, money):
    self.parent1money = money
    print("Parent1 init")

    class Parent2:
    def __init__(self, name):
    self.parent2name = name
    print("Parent2 init")

    class Child(Parent1, Parent2):
    def __init__(self, money, name):
    # I am calling parents explicitly
    Parent1.__init__(self, money)
    Parent2.__init__(self, name)

    obj = Child(1000, "test")]

    Continue reading...

Compartilhe esta Página