Clear, practical technology insights BSOD Code Lookup · Windows Error Code Lookup · Wi-Fi Troubleshooting · PC Troubleshooting Checklist

Class Inheritance in Python (Part 2)

Explore class inheritance in python (part 2) with clear explanations, practical guidance, and useful tips to avoid common mistakes.

Table of Contents

Class Inheritance in Python (Part 2) is easier to approach with a clear overview and reliable steps. This guide organizes the essential information, highlights practical details, and explains what to check along the way.

Key Takeaways

  • Review i. Multi-inheritance.
  • Explore II. Find attributes with inheritance.
  • Understand III. MRO.

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 instance:

The result of Child.bases in this case is:

II. Find Attributes with Inheritance

Logically, the process of finding attributes is as follows:

  • First, look up attributes in local dict.
  • If not, continue looking in the class dict.
  • Look in the classes in mro if it still doesn't exist.

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.

Use the syntax ".__mro__". This helps you view the MRO of a class. For instance:

Output:

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

  • 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).
  • 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.

Please try to brainstorm and explain the MRO of class E below: This helps you understand better.

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 instance:

NoisyPerson's MRO:

Try calling the talks method of the NoisyPerson instance:

Output:

Since NoisyPerson itself doesn't have this method, it will look for it in the next class in the MRO, Noisy when the girl's talk() method is called. 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 this guide!

FAQ

What should I know first about Class Inheritance in Python (Part 2)?

In the previous part, I mentioned the basic properties of classes in Python.

How do I get the best results with Class Inheritance in Python (Part 2)?

Use current software or equipment, follow the steps in order, review the recommended settings, and test one change at a time so you can identify what improves the result.

What should I do if Class Inheritance in Python (Part 2) doesn't work as expected?

Check compatibility, permissions, connectivity, and version-specific settings. Restart the relevant device or app, then repeat the process carefully before trying a more advanced fix.

Discussion

Reader Comments 0

Sign in with email or Google to join the discussion.