Multiple Inheritance in Python

In the previous lesson, we learned about Inheritance in object-oriented programming of Python language. Inheritance allows us to declare the new class to re-use the parent class's functions and attributes and extra functions. In this article, Quantrimang will continue the lesson with the theme Inheritance but at a deeper level, which is the Inheritance and the order of method access of the parent classes (Method Resolution Order).

Multiple Inheritance in Python Picture 1

Multiple Inheritance

Like C ++, in Python a class can be defined from multiple parent classes. This is called multiple inheritance.

Syntax:

 class LopCha1: 
pass

class LopCha2:
pass

class LopCon(LopCha1, LopCha2):
pass

Multiple Inheritance in Python Picture 2

Subclasses are defined from multiple parent classes and inherit the characteristics of both classes.

Parent classes can have the same properties or methods. The subclass will prioritize inheriting the attribute, the class method is first in the inheritance list.

Multi-level inheritance (Multilevel Inheritance)

In addition to being able to inherit from parent classes, you can also create new subclasses that inherit previous subclasses. This is called multi-level inheritance (Multilevel Inheritance).

In this case, the properties of the parent class and the previous subclass will be inherited by the new subclass.

Syntax:

 class LopCha: 
pass

class LopCon1(LopCha):
pass

class LopCon2(LopCon1):
pass

Multiple Inheritance in Python Picture 3

LopCon1 inherits LopCha, LopCon2 inherits LopCon1.

Method Resolution Order (Method Resolution Order)

Class is derived from object. In a multiple inheritance scenario, any attributes that need to be retrieved will first be searched in the current class. If not found, search continues to the first parent class and left to right.

So the order of access will be [LopCon, LopCha1, LopCha2, object].

This order is also called linearization of LopCon and the set of rules used to find this order is called the Method Access Order (MRO).

In simple terms, MRO is used to display lists / tuples of parent classes of a particular class.

MRO is used in two ways:

  1. __mro__: returns a tuple
  2. mro (): returns a list.
 >>> LopCon.__mro__ 
( , ( ,
,
,
)

>>> LopCon.mro()
[ , [ ,
,
,
]

Here is a complex inheritance example and its visual display along with the MRO.

Multiple Inheritance in Python Picture 4

 class X: pass 
class Y: pass
class Z: pass

class A(X,Y): pass
class B(Y,Z): pass

class M(B,A,Z): pass

# Output:
# [ , , # [ , , # [ , ,
# , , # , , # , ,
# , , # , , # , ,
# ]
# ]

See more:

  1. More than 100 Python exercises have solutions (sample code)
  2. Free online Python interpreter
  3. Some tools run Python on Android

Last lesson: Inheritance (Inheritance) in Python

Next lesson: Overload operator in Python

3.5 ★ | 2 Vote

May be interested

  • Programming blockchain part 3: Python programming languagePhoto of Programming blockchain part 3: Python programming language
    guido van rossum, a dutch programmer, created python in 1991. python is based on a simple philosophy: minimalist. one of the interesting things about python is that it combines simplicity into a programming language by using spaces to denote code blocks instead of curly braces or keywords.
  • Stack operator in PythonPhoto of Stack operator in Python
    you can change the meaning of the operator in python depending on the operand used and we call that operator overloading. quantrimang will learn more about this content through the article. invites you to read the track.
  • Complex () function in PythonPhoto of Complex () function in Python
    in python, the complex () function returns a complex number when the user provides a virtual and real part, or turns a string into a complex number. how does the complex () function syntax, what parameters do it have, and how is it used? invites you to read the track.
  • The compile () function in PythonPhoto of The compile () function in Python
    the compile () function returns a code object in python from the specified source. so how to use compile () function? please find out in this article.
  • The delattr () function in PythonPhoto of The delattr () function in Python
    the delattr () function in python is used to delete an attribute from the specified object. how does the delattr () function have a syntax, what parameters do you have, please find out in quantrimang in this section.
  • The dict () function in PythonPhoto of The dict () function in Python
    in python, the dict () function creates a dictionary type value. quantrimang will learn more about the syntax, parameters and usage of the dict () function through this article. invites you to read the track.