Fix the Inheritance Bug
Intermediate · Fix the bug · from Python Inheritance: Subclasses, Polymorphism, and super()
From the lesson: Python Inheritance: Subclasses, Polymorphism, and super()
Think about how scientists classify animals. A Dog is a type of Mammal, which is a type of Animal. Each level inherits traits from the one above it: all animals breathe, all mammals are warm-blooded, and all dogs bark.
Read the lessonThis code has a bug causing an AttributeError. Fix it so it prints:
Buddy (Golden Retriever) - Friendlyclass Animal:
def __init__(self, name):
self.name = name
class Dog(Animal):
def __init__(self, name, breed, temperament):
self.breed = breed
self.temperament = temperament
def __str__(self):
return f'{self.name} ({self.breed}) - {self.temperament}'
dog = Dog('Buddy', 'Golden Retriever', 'Friendly')
print(dog)
More Inheritance exercises
- Create a Vehicle SubclassBeginner
- Override the describe() MethodBeginner
- Predict the super() OutputIntermediate
- Build a Shape Area CalculatorIntermediate
- Build a Notification SystemAdvanced
All Python exercises · 50 practice problems · Object-Oriented Programming quiz