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
ᴀʀᴍᴀɴ
4,5368 gold badges41 silver badges61 bronze badges
asked Feb 14, 2017 at 14:52
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
answered Feb 14, 2017 at 14:58
Sign up to request clarification or add additional context in comments.

Comments

4

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

answered Feb 14, 2017 at 14:56

1 Comment

Does Dad allow that though?

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.