3

How can I make a common subclass initialize all parent classes it's inheriting from?

class Mom(object):
    def __init__(self):
        print("Mom")

class Dad(object):
    def __init__(self):
        print("Dad")

class Child(Mom, Dad):
    def __init__(self):
        super(Child, self).__init__()

c = Child() #only prints Mom
1
  • This task really doesn't make any sense. Of course, you can call in all inheritance sequence, but still - why you need that? Commented Feb 14, 2017 at 14:58

2 Answers 2

5

They are missing the super() calls in the Mom and Dad classes that are required for co-operative subclassing to work.

class Mom(object):
    def __init__(self):
        super(Mom, self).__init__()
        print("Mom")

class Dad(object):
    def __init__(self):
        super(Dad, self).__init__()
        print("Dad")

class Child(Dad, Mom):
    def __init__(self):
        super(Child, self).__init__()
c = Child() # Mom, Dad
Sign up to request clarification or add additional context in comments.

Comments

4

You need to call super in Mom's __init__ as well.

1 Comment

Does Dad allow that though?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.