Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the principle of python multi-inheritance breadth-first C3 algorithm

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces "what is the principle of python multi-inheritance breadth-first C3 algorithm". In daily operation, I believe that many people have doubts about the principle of python multi-inheritance breadth-first C3 algorithm. The editor consulted all kinds of data and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "what is the principle of python multi-inheritance breadth-first C3 algorithm". Next, please follow the editor to study!

Python multi-inheritance is more complex, python2's multi-inheritance search order is depth first, and pyhon3's multi-inheritance search order is the breadth priority of C3 algorithm.

C3 algorithm principle:

Every time the python interpreter encounters a class, it adds the inheritance sequence of its parent class to its own sequence according to the principle of breadth first.

First, add your own class to this sequence, and then judge the elements of the inherited sequence in turn.

If an element is not in another sequence or if it is the first of all inherited sequences, extract the element to this sequence.

If the element does not meet step 3, skip it to find the next element and proceed to step 3.

Loop through 3-4 steps until all elements of the inherited sequence are extracted to this sequence.

Case study:

Code: class F:print ('F') class G:print ('G') class D (F): print ('D') class E (G): print ('E') class B (D, E): print ('B') class C (E): print ('C') class A (B, C): print ('A') explanation:

Every time the interpreter encounters a class, it will add the inheritance sequence of its parent class to its own sequence according to the principle of breadth first of C3 algorithm.

First, class F [Fpeno].

Second, class G [GMAO].

Third, class D [D] + class F [FMago], single inheritance: [DFO].

4. Class E [E] + class G [GMago], single inheritance: [EGO].

Category B [B], Class D [DFO] + Class E [EGO]:

In the first step, the D of [DFO] is not in other sequences, and this sequence is extracted-- [BD], [FO] + [EGO].

In the second step, the F of [FO] is not in other sequences, and this sequence is extracted-- [BDF], [O] + [EGO].

The third step, the O of [O] is in another sequence and is not the first, skip it and look back-- [BDF], [O] + [EGO]

In the fourth step, the E of [EGO] is not in other sequences, and this sequence is extracted-- [BDFE], [O] + [GO].

In the fifth step, the G of [GO] is not in other sequences, and the sequence is extracted-- [BDFEG], [O] + [O].

Step 6, O of [O] is the first of all inherited sequences, extract and put into this sequence-- [BDFEGO], [] + []

In the seventh step, all inheritance sequences are empty and the operation is complete-- [BDFEGO]

The result of C3 algorithm: [BDFEGO].

Class C [C] + class E [EGO], single inheritance: [CEGO].

Class A [A], Class B [BDFEGO] + Class C [CEGO]:

In the first step, the B of [BDFEGO] is not in other sequences, and this sequence is extracted-- [AB], [DFEGO] + [CEGO].

In the second step, the D of [DFEGO] is not in other sequences, and this sequence is extracted-- [ABD], [FEGO] + [CEGO].

In the third step, the F of [FEGO] is not in other sequences, and this sequence is extracted-- [ABDF], [EGO] + [CEGO].

Step 4, the E of [EGO] is in another sequence and is not the first, skip it and look back-- [ABDF], [EGO] + [CEGO]

Step 5, the G of [EGO] is in another sequence and is not the first, skip it and look backwards-- [ABDF], [EGO] + [CEGO]

Step 6, the O of [EGO] is in another sequence and is not the first, skip it and look backwards-- [ABDF], [EGO] + [CEGO]

In the seventh step, the C of [CEGO] is not in other sequences, and this sequence is extracted-- [ABDFC], [EGO] + [EGO].

In the eighth step, E of [EGO] is the first of all inherited sequences. Extract and put into this sequence-- [ABDFCE], [GO] + [GO].

Step 9, G of [GO] is the first of all inherited sequences, extracted and put into this sequence-- [ABDFCEG], [O] + [O]

Step 10, O of [O] is the first of all inherited sequences, extracted and put into this sequence-- [ABDFCEGO], [] + []

Step 11, except for this sequence, all the sequences are empty, and the operation is completed-- [ABDFCEGO]

The result of C3 algorithm: [ABDFCEGO].

Test code

The breadth-first of C3 algorithm is a bit complicated and difficult to understand, so python3 provides a simple query function-- class name .mro ().

Print (A.mro ()) out: [,]

You can see that the result printed by A.mro () is consistent with the operation result ABDFCEGO of the above reasoning.

At this point, the study on "what is the principle of python multi-inheritance breadth-first C3 algorithm" is over. I hope to be able to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report