Fix the Cooperative __init__
Advanced · Fix the bug · from Python Multiple Inheritance and MRO: How It Actually Works
From the lesson: Python Multiple Inheritance and MRO: How It Actually Works
Think about how you inherit traits from your parents. You might have your mom's eyes and your dad's sense of humor. You didn't pick one parent - you got traits from both.
Read the lessonThis code uses multiple inheritance but the __init__ methods don't cooperate properly. Fix it so the output is:
Alice, age 25Hint: use **kwargs to pass remaining arguments up the MRO chain.
class Base:
def __init__(self):
pass
class Named(Base):
def __init__(self, name):
self.name = name
class Aged(Base):
def __init__(self, age):
self.age = age
class Person(Named, Aged):
pass
p = Person(name='Alice', age=25)
print(f'{p.name}, age {p.age}')
More Multiple Inheritance exercises
- Create a Hybrid ClassIntermediate
- Predict the MROAdvanced
- Build a TimestampMixinAdvanced
- Predict the super() ChainAdvanced
All Python exercises · 50 practice problems · Object-Oriented Programming quiz