Skip to content
Tutorials Center
My learning

Topics

Python Reference ยท Classes and Special Methods

Inheritance and the method resolution order

Concept guide to Python inheritance: subclasses, overriding, super(), multiple inheritance and how the method resolution order (MRO) is computed.

Inheritance lets a class (the subclass) reuse and specialize another class (the base class). A subclass gets every attribute and method of its base, can override any of them, and can extend them by calling the base version through super().

When Python looks up obj.name, it searches the instance, then the class, then the base classes in a fixed order called the method resolution order (MRO). With single inheritance the order is simply child, parent, grandparent, ending with object. With several bases Python computes it with the C3 linearization algorithm, which keeps each class before its bases and keeps the order in which you listed the bases.

Overriding and super()

Python Overriding and extending

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

super() does not mean "my parent". It means "the next class after this one in the MRO of the instance's type". That difference matters with multiple inheritance.

Multiple inheritance and the MRO

Python The diamond

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Because D.__mro__ is D, B, C, A, object, each hello runs exactly once. The call from B goes to C, not to A, even though B only inherits from A. This cooperative style works when every method calls super().

Key rules

  • Lookup order is always type(obj).__mro__, left to right. Read it with Cls.__mro__ or Cls.mro().
  • Base order matters. class D(B, C) puts B before C. If no consistent order exists, class creation raises TypeError: Cannot create a consistent method resolution order (MRO).
  • super() without arguments works inside methods; it uses the class where the method is defined and the first argument.
  • Always call super().__init__() in a subclass __init__ unless you deliberately skip base setup.
  • Mixins. Small classes that add one behavior (logging, comparison, serialization) are listed before the main base: class Report(JsonMixin, Base).
  • Prefer composition when the relationship is "has a" rather than "is a". Deep hierarchies are hard to change.
  • Checks: isinstance(obj, Cls) is true for subclasses; issubclass(A, B) compares classes; both accept tuples.
  • Overriding vs. shadowing. Assigning self.speak = ... on an instance hides the class method for that instance only.
  • Attribute lookup caveat. Instance attributes win over class attributes, except for data descriptors such as properties.
  • Python 3 only. All classes derive from object; there are no "old-style" classes.