Python Basics-the principle of inheritance implementation

Source: Internet
Author: User

Python Basics-the principle of inheritance implementation

1 Inheritance Order

Class A (object):    def Test (self):        print ("From A") class B (a):    def Test (self):        print (' From B ') class C (a) :    def Test (self):        print ("From C") class D (B):    def Test (self):        print (' From D ') class E (C):    def Test ( Self):        print (' From E ') class F (d,e):    # def test (self):    #     print (' from F ')    passf1=f () f1.test () Print (f.__mro__) #只有新式才有这个属性可以查看线性列表, the classic class does not have this property # new class inheritance Order: f->d->b->e->c->a# Classic class inheritance order: f->d->b- The unification of >a->e->c#python3 is the new class and the classic class in the new type #pyhon2 want $

  

2 inheritance principles (how Python implements Inheritance)

How python actually implements inheritance, for each class you define, Python calculates a list of method parsing orders (MRO), which is a simple linear sequential list of all base classes, such as

>>> F.mro () #等同于F. __mro__[<class ' __main__. F ';, <class ' __main__. D ';, <class ' __main__. B ';, <class ' __main__. E ';, <class ' __main__. C ';, <class ' __main__. A ';
<class ' object ';]

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 

Merge the MRO list of all the parent classes and follow these 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 valid choices for the next class, select the first parent class

3 Calling Parent class method in subclass (Super () method)

Subclass inherits the method of the parent class, and then wants to modify it, notice that it is based on the original modification, then you need to call the parent class's method in the child class

Method a parent class name. Parent class Method ()

Class people:    def __init__ (self,name,sex,age):        self.name=name        self.sex=sex        self.age=age    def Walk (self):     print ('%s was walking '%self.name) class Chinese (people):    country= ' China '    def __init__ (self, Name,sex,age,language= ' chinses '):        people.__init__ (self,name,sex,age)        self.language=language    def Walk (self,x):        passc=chinese (' xiaojing ', ' Male ', ') print (c.name,c.sex,c.age,c.language)

Method two super () method

Class people:    def __init__ (self,name,sex,age):        self.name=name        self.age=age        self.sex=sex    def Walk (self):        print ('%s was walking '%self.name) class Chinese (people):    country= ' China '    def __init__ (self, Name,sex,age,language= ' Chinese '):        super (). __init__ (Name,sex,age)  #super () binding calls the parent class method        self.language= Language    def Walk (self,x):        super (). Walk ()        print ("----> Subclass X", X) C=chinese (' EGG ', ' Male ', ' 20 ') C.walk (123)

without the super-induced massacre.

#每个类中都继承了且重写了父类的方法class A:    def __init__ (self):        print (' A's construction method ') class B (a):    def __init__ (self):        Print (' Construction method of B ')        a.__init__ (Self) class C (A):    def __init__ (self):        print (' constructor of C ')        a.__init__ (self) Class D (B,c):    def __init__ (self):        print (construction method of ' D ')        b.__init__ (self)        c.__init__ (    self) Passf1=d () print (d.__mro__) #python2中没有这个属性

When you use the super () function, Python continues to search for the next class on the MRO list. As long as each redefined method uses super () and only calls it once, the control flow will eventually traverse the complete MRO list, and each method will only be called once (note : All properties using Super Call are looked back from the MRO list's current location. Do not look at the code to find the inheritance relationship, be sure to see the MRO list )

#每个类中都继承了且重写了父类的方法class A:    def __init__ (self):        print (' A's construction method ') class B (a):    def __init__ (self):        Print (construction method of ' B ')        super (b,self). __init__ () class C (A):    def __init__ (self):        print (' construction method of C ')        super (C, Self). __init__ () class D (B,c):    def __init__ (self):        print (' construction method of D ')        super (d,self). __init__ () F1=d () Print (d.__mro__) #python2中没有这个属性

Python Basics-the principle of inheritance implementation

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.