Class Inheritance in Python (Part 2)

Class Inheritance in Python (Part 2) Picture 1

In the previous part, I mentioned the basic properties of classes in Python. In this part, I will mention the advanced properties and characteristics of classes.

I. Multi-inheritance

Unlike Java — a language that only allows inheritance of a single class for a class, Python allows us to inherit multiple classes for a class. For example:

The result of Child.bases in this case is:

II. Find attributes with inheritance

Logically, the process of finding attributes is as follows:

  1. First, look up attributes in local dict
  2. If not, continue looking in the class dict
  3. If it still doesn't exist, look in the classes in mro.

I will clarify the last point in the following parts of the article.

III. MRO

MRO stands for Method Resolution Order. It is an inheritance chain that Python calculates and stores in the MRO attribute in the class. As mentioned above, when looking for attributes, Python will go through the elements in MRO in turn.

To view the MRO of a class, use the syntax ".__mro__". For example:

Output:

Python uses cooperative multiple inheritance to enforce some rules about class order:

  1. Children are always checked before parents (children classes are always checked before parents classes) Parents (if multiple) are always checked in the order listed (parents classes are always checked in the order listed)
  2. As in the above example, after checking at E, class D will be checked as D is listed first among the two base classes. Then, B — parent class of D will be checked. Finally, class C and its parent — A will be checked.

To understand better, please try to brainstorm and explain the MRO of class E below:

This algorithm is called "C3 Linearization Algorithm" but to keep it simple and easy to understand, imagine the order of escape when an incident like a house fire or a ship sinking occurs: "Children first, followed by parents".

IV. Examples of multi-inheritance

super() will call the next class of the current class in the MRO

For example:

NoisyPerson's MRO:

Try calling the talks method of the NoisyPerson instance:

Output:

When the girl's talk() method is called, since NoisyPerson itself does not have this method, it will look for it in the next class in the MRO, Noisy. Noisy has a talk() method that will be executed. super().talk() will look for the talk() method of the next class in the MRO - Person (return 'alo alo'). This string 'alo alo' will be upper()ed and returned. Therefore the output returned is "ALO ALO"

Now what if we swap the two parent classes?

Rerun the talks method

Output:

Here is NoisyPerson's MRO now:

We see that the Person class is checked before Noisy, while Person has a talks() method so it will be executed.

V. Conclusion

Through the two parts on the topic of class, I hope you have more knowledge about class and can apply it to your work. Please look forward to the next articles on the topic of Python on the tech blog TipsMake!

4 ★ | 2 Vote

May be interested

  • Multiple choice quiz about Python - Part 3Multiple choice quiz about Python - Part 3
    today's topic quantrimang wants to challenge you is about file and exception handling in python. let's try the following 15 questions!
  • Multiple choice quiz about Python - Part 2Multiple choice quiz about Python - Part 2
    to help readers add to their interesting knowledge of python objects and classes, below the network administrator will send you multiple choice questions on this topic. please try.
  • Multiple choice test on Python - Part 11Multiple choice test on Python - Part 11
    python's multiple-choice questions series help you update and review useful knowledge for your work and learn your python programming language.
  • Multiple choice quiz about Python - Part 6Multiple choice quiz about Python - Part 6
    to help readers add to their knowledge of python's built-in functions, the network administrator will send you multiple choice questions on this topic. please try.
  • Current date and time in PythonCurrent date and time in Python
    how to get the current date and time in python? there are many different ways to do this, and in this article we will apply the class date, class time in the datetime module to display the current date in python.
  • Multiple choice quiz about Python - Part 8Multiple choice quiz about Python - Part 8
    python's built-in function of multiple-choice questions helps you update and review useful knowledge for your work and learn your python programming language.
  • Multiple choice quiz about Python - Part 5Multiple choice quiz about Python - Part 5
    if you are interested in learning about the python programming language, the following quiz of network administrator will provide useful knowledge for your learning.
  • Multiple choice quiz about Python - Part 1Multiple choice quiz about Python - Part 1
    python is an object-oriented, high-level, powerful, easy-to-learn programming language. to help you read more interesting knowledge about python programming language, below the network administrator will send you a very interesting multiple choice questionnaire on this topic. please try.
  • Multiple choice quiz about Python - Part 9Multiple choice quiz about Python - Part 9
    how much do you know about python functions? keep testing your knowledge with quantrimang through the following multiple choice questions!
  • Array in PythonArray in Python
    arrays are a fundamental part of all programming languages, it is a collection of elements of a single data type, for example, integer arrays, string arrays. however, in pythong, there is no original array data structure. so we use python lists instead of arrays.