Base Method Calls in Python Multiple Inheritance

Python allows multiple inheritance. Method resolution order in Python is a quite interesting topic, and it's possible to read more here. The idea of this blog post is to show options when it comes to calling methods from the base classes.

If a class is extended from multiple classes like in the example below

class Base1():
    def print(self):
        print("Base1")
        print(self)

class Base2():
    def print(self):
        print("Base2")
        print(self)

both calls

class Derived(Base1, Base2):
    def print(self):
        # Calls Base1 method.
        super().print()
        # Calls Base1 method.
        super(Derived, self).print()

are going to call print method from Base1 class. If a caller wants to call the print method from Base2 class, an explicit call is required.

class Derived(Base1, Base2):
    def print(self):
        # Calls Base2 method.
        Base2.print(self)

The full Python script can be found here.

Resources

To checkout other posts, please go to the home page. Thank you for reading!