Python Hard 1
š§ Code Challenge
class Animal:
def __init__(self, name):
self.name = name
def speak(self):
return f"{self.name} makes a sound."
class Cat(Animal):
def __init__(self, name, breed):
super().__init__(name)
self.breed = breed
def speak(self):
return f"{self.name} the {self.breed} says 'Meow!'"
class Dog(Animal):
def speak(self):
return f"{self.name} says 'Woof!'"
pets = [Cat("Whiskers", "Siamese"), Dog("Buddy"), Cat("Luna", "Persian")]
for pet in pets:
print(pet.speak())
Explain what this function does. Speak clearly and use keywords.