Request to help me with python inheritance concept

  class, inheritance, object, python

Program: class Pets: animals = [] def init(self, animals): self.animals = animals def walk(self): for animal in self.animals: print(animal.walk()) class Cat: is_lazy = True def init(self, name, age): self.name = name self.age = age def walk(self): return f'{self.name} is just walking around’

class Simon(Cat): def sing(self, sounds): return f'{sounds}’ class Sally(Cat): def sing(self, sounds): return f'{sounds}’ class Micheal(Cat): def sing(self, sounds): return f'{sounds}’ animal_1 = Simon(name="Simon", age=14) animal_2 = Sally(name="Sally", age=15) animal_3 = Micheal(name="Micheal", age=11) my_cats = [animal_1, animal_2, animal_3] my_pets = Pets(my_cats) my_pets.walk()

Output: Simon is just walking around Sally is just walking around Micheal is just walking around

When I am calling my_pets.walk() how it is returning a function that is under cat class How animal.walk() calling this function def walk(self): return f'{self.name} is just walking around’

Source: Python Questions

LEAVE A COMMENT