Back
Close

Journey to Master Python (WIP)

ageekymonk
34.1K views
Previous: Introspection

Multiple Inheritance

Multiple Inheritance with two non overlapping parents

Multiple Inheritance is easy if we inherit from two parent class, each without any overlapping methods

Multiple Inheritance with overlapping methods

If there are overlapping methods, the method of first parent class will be called. In the below example, since we added the class Father as the first argument in class Son(Father, Mother), the method in class Father will be called. If it had been class Son(Mother, Father) then method in class Mother will be called.

Multiple Inheritance with Diamond Structure

To solve this python computes mro, method resolution order for a class. This can be found by __mro__. This gives a list of parent classes linearized. So python checks for next in line based on the __mro__.

super follows the mro as well. It does not call the parent of a class. It calls next in line based mro.

The algorithm used for computing the mro is C3 Linearization Algorithm. More details on the algorithm in mro

Create your playground on Tech.io
This playground was created on Tech.io, our hands-on, knowledge-sharing platform for developers.
Go to tech.io