Python class Five: multiple-inheritance MRO order

Source: Internet
Author: User


This series of narration, part or example from <<python Core Programming second Edition >>


Python classes have classic and modern classes, and in multiple inheritance, the order in which the methods are inherited is different depending on the type of class.


First, the Classic class:


The classic class is characterized by not inheriting from any class:


#coding: Utf-8class p_1:def foo (self): print ' called P1-foo () ' Class P_2:def foo (self): print ' called        P2-foo () ' Def Bar (self): print ' called P2-bar ' class C_1 (p_1,p_2): Passclass c_2 (p_1,p_2): Def bar (self): print ' called C2-bar () ' Class C_3 (c_1,c_2): PASSC3 = C_3 () C3.foo () C3.bar ()

650) this.width=650; "Src=" Http://s2.51cto.com/wyfs02/M00/88/5E/wKiom1fyiQfiKszcAAAsq7mGBgE911.jpg-wh_500x0-wm_3 -wmp_4-s_2401073039.jpg "title=" 1.JPG "alt=" Wkiom1fyiqfikszcaaasq7mgbge911.jpg-wh_50 "/>


This inheritance relationship is shown in


The multiple inheritance of the classic class, the order in which the subclasses search for methods that inherit from the parent class are: depth first, left to right.


So, for C3, this example comes from class c_3.


C_3 does not implement the Foo and bar methods on its own.


Then, follow the principle of depth first, from left to right.


For the Foo method:

Find yourself first, I did not implement Foo, and then look up c_2, c_2 also did not implement Foo, continue to find C_1,c_1 also did not implement Foo, continue to find p_1,p_1 implementation of the Foo method.

Then the output of C_3.foo () is: called P1-foo ()


For the Bar Method:

Find yourself first, do not have bar, and then look up c_1,c_1 did not implement bar, continue to find P_1,p_1 implemented bar method.

Then the output of C_3.bar () is: called P2-bar


And if it's a new class:


The MRO search order is breadth-first, from left to right


The same code (just declared with the new Class):


#coding: Utf-8class p_1 (object): Def foo (self): print ' called P1-foo () ' Class P_2 (object): Def foo (self):    print ' called P2-foo () ' Def Bar (self): print ' called P2-bar ' class C_1 (p_1,p_2): Passclass c_2 (p_1,p_2): def bar (self): print ' called C2-bar () ' Class C_3 (c_1,c_2): PASSC3 = C_3 () C3.foo () C3.bar ()


For C3.foo ()


First find C3 himself, C_3 himself did not implement Foo, continue to find C_1,c_1 also did not implement Foo, then find c_1 brother C_2,c_2 also did not implement Foo, then continue to find P_1,p_1 realized Foo

Therefore, the output of the C3.foo is called P1-foo ()


For C3.bar ()


First find C3 himself, C_3 himself did not realize bar, then continue to find c_1,c_1 himself also did not realize bar, then continue to find c_1 Brother C_2,c_2 realized bar method.


Then the output of the C3.bar is called C2-bar ()


Summarize:

For the new class, the breadth first, from left to right, is searched.

For classic classes, depth-first, left-to-right order search.


Python class Five: multiple-inheritance MRO order

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.