Deep inheritance chains are brittle. When requirements change, you end up fighting the hierarchy.
# dirty — inheritance chain
class Animal:
def __init__(self, name):
self.name = name
class Dog(Animal):
def bark(self):
return f'{self.name} says: woof'
class Cat(Animal):
def meow(self):
return f'{self.name} says: meow'Refactor using composition: create create_animal(name, sound) that returns a dict with a speak callable. No classes, no inheritance.solve(name, sound) calls create_animal(name, sound)['speak']().
solve('Rex', 'woof') → 'Rex says: woof'
Sample tests