Skip to content
Laviyx Learn

Try

Python Lab · Experiment 29

Method Resolution Order (MRO) in multiple levels of inheritance

PythonWeek 6JNTUH R22 · CS206ES

Aim

To write a Python program that shows the Method Resolution Order (MRO) for multilevel inheritance, multiple inheritance and diamond inheritance, by printing each class's __mro__ and showing which class's method runs.

Algorithm

  1. Multilevel inheritance: define Grandparent, Parent(Grandparent) and Child(Parent). Override role() in Parent only, and define family() in Grandparent only.
  2. Print the MRO of Child, then call role() and family() on a Child object to see which class each method comes from.
  3. Multiple inheritance: define Father and Mother, both with a method skill(), and two classes that inherit from both in opposite orders.
  4. Print the MRO of each and call skill() to show that the base class listed first wins.
  5. Diamond inheritance: define A, then B(A) and C(A), then D(B, C). Each show() method prints its class name and calls super().show().
  6. Print D.mro() and call show() on a D object. The calls follow the MRO, D → B → C → A, and A's method runs only once.

Program

Python
def print_mro(cls):
    names = [c.__name__ for c in cls.__mro__]
    print(f"MRO of {cls.__name__}:", " -> ".join(names))


# 1. Multilevel inheritance: Grandparent -> Parent -> Child
class Grandparent:
    def role(self):
        return "role() from Grandparent"

    def family(self):
        return "family() from Grandparent"


class Parent(Grandparent):
    def role(self):
        return "role() from Parent"


class Child(Parent):
    pass


print("1. Multilevel inheritance")
print_mro(Child)
kid = Child()
print(kid.role())
print(kid.family())


# 2. Multiple inheritance: one class with two parents
class Father:
    def skill(self):
        return "skill() from Father: driving"


class Mother:
    def skill(self):
        return "skill() from Mother: cooking"


class Son(Father, Mother):
    pass


class Daughter(Mother, Father):
    pass


print("\n2. Multiple inheritance")
print_mro(Son)
print(Son().skill())
print_mro(Daughter)
print(Daughter().skill())


# 3. Diamond inheritance: B and C both inherit from A, D from B and C
class A:
    def show(self):
        print("  show() in A")


class B(A):
    def show(self):
        print("  show() in B")
        super().show()


class C(A):
    def show(self):
        print("  show() in C")
        super().show()


class D(B, C):
    def show(self):
        print("  show() in D")
        super().show()


print("\n3. Diamond inheritance")
print("D.mro() gives:", D.mro())
print_mro(D)
print("Calling D().show():")
D().show()

Output

Output
1. Multilevel inheritance
MRO of Child: Child -> Parent -> Grandparent -> object
role() from Parent
family() from Grandparent

2. Multiple inheritance
MRO of Son: Son -> Father -> Mother -> object
skill() from Father: driving
MRO of Daughter: Daughter -> Mother -> Father -> object
skill() from Mother: cooking

3. Diamond inheritance
D.mro() gives: [<class '__main__.D'>, <class '__main__.B'>, <class '__main__.C'>, <class '__main__.A'>, <class 'object'>]
MRO of D: D -> B -> C -> A -> object
Calling D().show():
  show() in D
  show() in B
  show() in C
  show() in A

Recorded by running this exact program.

How it works

When you call a method on an object, Python searches the classes in the object's Method Resolution Order and runs the first method it finds with that name. Every class has its MRO in ClassName.__mro__ (a tuple) and ClassName.mro() (a list). The last class is always object, the base of every class in Python 3.

In multilevel inheritance the MRO is simply the chain upwards: Child, Parent, Grandparent, object. kid.role() finds role in Parent first, while family is found only in Grandparent.

In multiple inheritance, the base classes are searched in the order they are written. Son(Father, Mother) uses Father's skill, and Daughter(Mother, Father) uses Mother's.

In the diamond, Python 3 uses the C3 linearization rule. It keeps the left-to-right order of the bases and puts a class after all its subclasses, so the MRO of D is D, B, C, A, object: A comes after both B and C. super() does not mean "my parent class"; it means "the next class in the MRO of the object". That is why super().show() inside B calls C.show() for a D object, and A.show() runs only once.

Common mistakes

  • Thinking super() in B always calls A. It calls the next class in the MRO of the actual object, which for a D object is C.

  • Expecting the diamond to search depth-first (D, B, A, C). Python 3 puts A after both B and C, because a class always comes after its subclasses in the MRO.

  • Writing the base classes in an order Python cannot keep, such as class E(A, B) when B is a subclass of A. Python raises a TypeError because no consistent MRO exists. List subclasses before their base classes.

  • Calling A.show(self) directly in both B and C. In a diamond this runs A's method twice. Use super().show() so each method runs once in MRO order.

Viva questions

Try to answer each one aloud before you open it.

  • What is the Method Resolution Order?

    It is the order in which Python searches a class and its base classes when looking for a method or attribute. Python runs the first match it finds in this order.

  • How can you see the MRO of a class?

    With ClassName.__mro__, which gives a tuple of classes, or ClassName.mro(), which gives a list. help(ClassName) also shows it.

  • What is the MRO of D(B, C) when B and C both inherit from A?

    D, B, C, A, object. A comes after both B and C, and object is always last.

  • Which algorithm does Python 3 use to compute the MRO?

    The C3 linearization algorithm. It keeps each class before its base classes and keeps the left-to-right order in which the bases are listed.

  • What is the diamond problem?

    When a class inherits from two classes that share one base class, the question is which path to search and whether the shared base's method runs once or twice. Python's MRO puts the shared base once, after both middle classes.

  • What does super() return in multiple inheritance?

    An object that looks up methods starting from the class after the current one in the MRO of the actual object, not necessarily the direct parent of the current class.

Learn the concepts

All Python Programming Lab experiments →