Inheritance and super()

class Dog(Animal) makes Dog a subclass. It gets every method of Animal, and can override any of them by defining a method with the same name.

super() reaches the parent's version. Two common uses:

  • In __init__, call super().__init__(name) so the parent sets up its own attributes. Forget it and self.name never exists.
  • In an override, call super().speak() and add to the result.

A Dog is also an Animal, so isinstance(rex, Animal) is True.

When Python looks up a method it walks the class's MRO (method resolution order), shown by Puppy.__mro__. Every class ends at object.

Keep hierarchies shallow. Two levels is plenty in most code, and the next page shows the usual alternative.

Run it, then remove the super().__init__ line and see what breaks.