How do python subclasses use MRO in multiple inheritance?

WBOY
Release: 2023-04-19 18:19:13
forward
1542 people have browsed it

Subclasses use the MRO mechanism in multiple inheritance

In Python, when defining a class, you can specify its parent class. A subclass inherits all the properties and methods of its parent class and can add its own unique properties and methods.

However, if a class has multiple direct parent classes, there may be properties and methods with the same name between these parent classes. In order to correctly call these properties and methods, Python uses an algorithm called "Method Resolution Order" (Method Resolution Order, MRO) to determine the search order of properties and methods.

Algorithm principle

In Python 2.x, MRO is implemented using the depth-first search algorithm (DFS). There are some problems with this algorithm that cause the method call sequence to not be correctly parsed in some cases. For example:

class A:
    def foo(self):
        print("A.foo")

class B(A):
    pass

class C(A):
    def foo(self):
        print("C.foo")

class D(B, C):
    pass

d = D()
d.foo()  # 输出"A.foo",而不是"C.foo"
Copy after login

In the above code, class D inherits class B and class C, and class C overrides the foo() method of class A. Therefore, when calling the foo() method of object d, theoretically the foo() method in class C should be called first. However, since Python 2.x uses the DFS algorithm, it will traverse class B first, then class C, and finally class A. Therefore, the foo() method in class A is ultimately called, not the foo() method in class C.

In order to solve this problem, Python 2.3 introduced the C3 algorithm, which uses a topological sorting algorithm to calculate the MRO list to ensure the correctness when calling methods. The basic principle of the C3 algorithm is as follows:

  • The MRO list of new-style classes (that is, classes that explicitly inherit object or implicitly inherit object) is calculated according to the breadth-first search (BFS) algorithm.

  • For each class, its MRO list should meet the following three conditions:

    • The MRO list of subclasses should be ranked At the front of the MRO list of the parent class.

    • If two parent classes appear in the MRO list of a child class, their relative order in the list must be consistent with their appearance in the direct parent class of the child class. The relative order is the same.

    • A class cannot appear more than twice in its MRO list.

This algorithm can correctly handle the situation in the above example code, thereby ensuring the correctness when calling the method.

View MRO list

In Python 3, you can view the MRO list of a class through the __mro__ attribute. For example:

class A:
    def foo(self):
        print("A.foo")

class B(A):
    pass

class C(A):
    def foo(self):
        print("C.foo")

class D(B, C):
    pass

print(D.__mro__)
Copy after login

The output result is:

(, <class '__main__.B'>, , <class '__main__.A'>, <class 'object'>)

Among them, <class '__main__.D '> represents class D itself, <class '__main__.B'> and <class '__main__.C'> represent the parent of class D respectively Classes B and C, <class '__main__.A'> represents the common parent class A of classes B and C, <class 'object'> represents all new-style classes base class. The order of this list is the order in which properties and methods are looked up when Python is running.

The above is the detailed content of How do python subclasses use MRO in multiple inheritance?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!