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
teddybear123
2,4446 gold badges26 silver badges40 bronze badges
-
This task really doesn't make any sense. Of course, you can call in all inheritance sequence, but still - why you need that?Sergius– Sergius2017年02月14日 14:58:01 +00:00Commented Feb 14, 2017 at 14:58
2 Answers 2
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
ᴀʀᴍᴀɴ
4,5368 gold badges41 silver badges61 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
You need to call super in Mom's __init__ as well.
answered Feb 14, 2017 at 14:56
Daniel Roseman
602k68 gold badges911 silver badges924 bronze badges
1 Comment
Constantin
Does Dad allow that though?
lang-py