MediumPro challengePython

Favor Composition Over Inheritance

PythonClean CodeSOLIDComposition

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

Test #1Dog speaks
Input: ["Rex","woof"]
Output: "Rex says: woof"
Test #2Cat speaks
Input: ["Whiskers","meow"]
Output: "Whiskers says: meow"
Test #3Any animal works
Input: ["Tweety","tweet"]
Output: "Tweety says: tweet"