0
class father:
    def __init__(self):
        print("Dad")

class mother:
    def __init__(self):
        print("Mom")
        
class child(father,mother):
    def __init__(self):
        super().__init__()
        print("Child")
        
child()

Output:

Dad
Child

How to do it correctly such that the mother is initialized as well?

I understand this problem has been addressed previously, but I can only find either long explanations about MRO or modifications to the father and mother class, which is in my case not allowed. I'm sure this problem is more trivial than that...

10
  • 1
    Add super().__init__ calls to all __init__s? Commented Dec 8, 2020 at 10:47
  • It's there in the child class Commented Dec 8, 2020 at 10:48
  • 1
    All __init__s. That means in father and mother as well. Right now your super call goes to the father's __init__, prints Dad and goes back to child. You need to add another super call there as well in order for it to go to the mother's __init__ as well Commented Dec 8, 2020 at 10:49
  • 1
    @HenryTjhia Why you don't recommend to use super()? That's the idiomatic way of calling the parent... Commented Dec 8, 2020 at 11:01
  • 1
    Calling super() isn't simply idiomatic, it the right way to do it, especially if multiple inheritance is involved. Commented Dec 8, 2020 at 11:10

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.