Learning Notes - python (Inheritance)

Posted by jyushinx on Wed, 26 Feb 2020 18:42:28 +0100

Learning Notes (Python Inheritance)

There are several names (parent-child, base-derived)
Let's start with a code demonstration:

 1 class father:
 2     def work(self):
 3         print("work>>>>>")
 5     def car(self):
 6         print("car>>>>>>>>>")
 8 class son(father):   #To inherit, you have to add a parent class
 9     def study(self):
10         print("study>>>>>>>>>>")
12 obj = son()    
13 obj.work()    #In the above method, work In self Is a formal parameter, which points to obj
14 #What's more important is that self Always point to the caller of the calling method (such as here) obj)
15 #work>>>>>


From the code above, you can see that obj is an object that calls a subclass at this time, but you can use the method of the parent class. The main reason is that the parent class is added after the subclass. If you do not add a parent class, you cannot call the method of the parent class and get an error. This form is called inheritance
(self always points to the caller of the calling method)

1. When inheriting a method of a parent class, if you do not want to call some methods of the parent class, you need to override them in the child class yourself.

(Override the work method in the parent class in the following classes, don't think too much about restrictions first)

 1 class son(father):
 2     def study(self):
 3         print("study>>>>>>>>>>")
 5     def work(self):
 6         print("s.work")     #Write it yourself (rewrite) without inheriting it
 8 #At this point, when calling the method, look first from the method in the subclass, if there is onebeforeCall, if not, go to the parent class to call
 9 obj = son()
10 obj.work()    #s.work


2. When inheriting a parent method, your child overrides it, but still wants to execute the method whose parent is overridden, do the following:

1 def work(self):
2     print("s.work")
3     super(son,self).work()   #Party I: Call super Method, super(The name of the current class, self).Method whose parent class is overridden())
4     father.work(self)        #Square 2: This method is an active call to the parent method, at this time self Requires us to actively add, indicating that the parent also implements its object methods

3. Like C++, python can inherit multiple parent classes

 1 class uncle:
 2     def video(self):
 3         print("uncle.video")   
 4 class father(uncle):
 5     def work(self):
 6         print("father.work")
 7 class mother:
 8     def car(self):
 9         print("mother.car")   
11 class son(father,mother):  
12     def study(self):
13         print("son.study") 


(1) Multiple parent inheritance starts from left to right:

obj = son()
obj.car()    # mother.car


(Find the car() method in the father class first, then jump to the mother class if none)

(2) If there is a parent above the inherited parent, then "always search to the end", then call, or inherit the next parent to find a method

obj = son()
obj.video()    # uncle.video


(3) The above two statements are based on the discussion that the ultimate parent is not the same, and the third judgment is based on the discussion that the ultimate parent is the same, coded as follows:

class aunt:
    def computer(self):

class uncle:
    def video(self):

class father(uncle):
    def work(self):

class mother(aunt):
    def car(self):

class son(father, mother):
    def study(self):

obj = son()
obj.computer()   #aunt.computer


The sequence of execution is as follows:

Red lines inherit upwards and black lines are the process of code execution
It's similar to (1) (2), but unlike (1) (2), when the method invoked is never found on the left, it doesn't always find the bottom. Instead, it goes back to the son subclass before the last parent, starts looking in the mother class, and finally points to the aunt class and finds its computer() method
(3) Summary: If you have a common parent, you will not always search to the end when looking for a method. Instead, you will search for the parent before the end and change to the original parent before the end. If you can't find it, you will find the parent after the end.) How to understand and remember

(4) Order of code execution when self. method () is used

(The code below modified a little)

class uncle:
    def video(self):
class father(uncle):
    def work(self):
    def car(self):
class mother:
    def car(self):
class son(mother,father):  
    def study(self):
obj = son()
obj.work()    #father.work  


When an object is looking for a method in the de-parent class, since the mother class does not have its method, after it is found and executed under the father class, there is also self.car(),This method should reasonably take precedence in the same class, but the result is mother.car. The simple reason is that when a self.method () is encountered, it should be returned to the class called by its object (the object that self points to, this code is obj) to execute its method call (in the case of the above code, obj.car() is called again).Then look for a method from its subclass, and then look for a method from its parent. The first parent, mother, has a car() method in the mother class, so call execution directly, over!).

(5) The u init_() method in the class is executed when the first one is found:

 1 class uncle:
 2     def __init__(self):
 3         print("uncle.init")
 4     def video(self):
 5         print("uncle.video")   
 6 class father(uncle):
 7     def __init__(self):
 8         print("father.init")
 9     def work(self):
10         print("father.work")
11 class mother:
12     def car(self):
13         print("mother.car")   
15 class son(father,mother):  
16     def study(self):
17         print("son.study") 
18 obj = son()   #father.init


(The above code father class also inherits a parent class, which has both u init_u methods, but traverses to the subclass father first, so the init method under its class is called first, and only the first u init_u method encountered is executed once each time the object of its class is executed)

Topics: Python