Python Fundamentals ยท Objects, Errors and Generators

Inheritance and super()

A Dog is an Animal; a Puppy is a Dog. Child classes reuse the parent's code, override what differs, and extend it with super().

inheritance.py

class Animal:
    def __init__(self, name):
        self.name = name

    def speak(self):
        return "..."

    def intro(self):
        return self.name + " says " + self.speak()

class Dog(Animal):
    def speak(self):            # overrides Animal.speak
        return "woof"

class Puppy(Dog):
    def speak(self):
        return super().speak() + " (squeaky)"   # extends the parent's version

pets = [Animal("generic"), Dog("rex"), Puppy("bit")]
for pet in pets:
    print(pet.intro())

Step by step

  1. Python starts building the class Animal. Its methods are stored, but none of them run yet.
  2. Python reads the class body: everything indented under class Animal belongs to it.
  3. The method __init__ is stored in Animal. It runs only when it is called on an object.
  4. The method speak is stored in Animal. It runs only when it is called on an object.
  5. The method intro is stored in Animal. It runs only when it is called on an object.
  6. The class Animal is now defined, ready to make objects from.
  7. Dog(Animal): Dog gets everything Animal has.
  8. Python reads the class body: everything indented under class Dog belongs to it.
  9. The method speak is stored in Dog. It runs only when it is called on an object.
  10. The class Dog is now defined, ready to make objects from.
  11. Puppy(Dog): a Puppy is a Dog, and so also an Animal.
  12. Python reads the class body: everything indented under class Puppy belongs to it.
  13. The method speak is stored in Puppy. It runs only when it is called on an object.
  14. The class Puppy is now defined, ready to make objects from.
  15. Three pets. Dog and Puppy have no __init__ of their own, so Animal's runs.
  16. A new key name with the value 'generic' is added to the Animal that self points to.
  17. Three pets. Dog and Puppy have no __init__ of their own, so Animal's runs.
  18. A new key name with the value 'rex' is added to the Dog that self points to.
  19. Three pets. Dog and Puppy have no __init__ of their own, so Animal's runs.
  20. A new key name with the value 'bit' is added to the Puppy that self points to.
  21. Three pets. Dog and Puppy have no __init__ of their own, so Animal's runs.
  22. The value it returned, {name: 'generic'}, is stored in pet.
  23. Python calls intro(), with self = {name: 'generic'}. It gets its own names.
  24. intro calls self.speak(). Which speak runs depends on the object.
  25. The function returns "...".
  26. speak() has finished, so its names are gone.
  27. intro() has finished, so its names are gone. It prints generic says ....
  28. The value it returned, {name: 'rex'}, is stored in pet.
  29. Python calls intro(), with self = {name: 'rex'}. It gets its own names.
  30. For the Dog, self.speak() finds Dog.speak first: "woof".
  31. The function returns "woof".
  32. speak() has finished, so its names are gone.
  33. intro() has finished, so its names are gone. It prints rex says woof.
  34. The value it returned, {name: 'bit'}, is stored in pet.
  35. Python calls intro(), with self = {name: 'bit'}. It gets its own names.
  36. Python calls speak(), with self = {name: 'bit'}. It gets its own names.
  37. super().speak() runs Dog's speak, then the Puppy adds to it.
  38. The function returns "woof".
  39. The function returns super().speak() + " (squeaky)" # extends the parent's version.
  40. speak() has finished, so its names are gone.
  41. intro() has finished, so its names are gone. It prints bit says woof (squeaky).
  42. One intro method, three answers: each object used the most specific speak it had.