Python New class inheritance Order

Source: Internet
Author: User

Inheritance Order

Breadth First principle:

classA:PassclassB (A):PassclassC (A):PassclassD (B):PassclassE (C):PassclassF (D, E):PassPrint("f.__mro__ ="F.__mro__)Print("F.mro () =", F.mro ())
View Code

Principles of inheritance

How Python actually implements inheritance:

    • For each class defined, Python calculates a list of method resolution orders (MRO), which is a simple linear sequential list of all base classes.
    • To implement inheritance, Python finds the base class from left to right on the MRO list until the first class that matches the property is found.
    • The construction of the MRO list is implemented by a C3 linearization algorithm. We're not going to delve into the math of this algorithm, it's actually merging all of the parent's MRO lists and following three guidelines:
    1. Subclasses are checked before the parent class.
    2. Multiple parent classes are checked according to their order in the list.
    3. If there are two legitimate choices for the next class, select the first parent class.

Python New class inheritance Order

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.