Python new skills get--look! Source! Code!

Source: Internet
Author: User

Last class together looked at the source of Threadingtcpserver and tornado, really exhausted ... Later thought carefully, understand the source code is not difficult, but need to clear some of the previous we have learned but easy to ignore the concept!

See Inheritance again

Before the real meal comes, have a little appetizer! Review the knowledge about class inheritance:

Let's look at the code above, this is a simple class inheritance, we can see the parent class base and subclass son, each of which has a testfunc method, when we instantiate the subclass of the object sonobj, we can see the initialization method in the yellow box called TestFunc, So what is the code that executes at this time of the class? I will tell you to execute the method inside the subclass, do not believe it, try it yourself, the code is below ~

Why is it? In fact, this is a circle around, so a lot of people around the dizzy, including me! Later think it is actually very easy, we look at the far right of the graph:

If so, do we understand? In fact, these two pieces of code represent a meaning, although son inherits the base class, the parent-child class has the same method, but because we instantiate the subclass of the object, so this in the initialization method of self. Testfunc,self refers to the object of the subclass, and of course the method in the subclass is called first. So although in the first example, the initialization method executes in the parent class, but still cannot change it is the essence of the subclass object, when we use self to call the TestFunc method, we always call the subclass method first. We can understand that although the son inherits the father's property, but spends the time, still must first spend oneself ~ ~ ~

1#_*_coding:utf-8_*_2__author__ =‘Eva_j‘3ClassBase (object):4Def__init__(self,name):5 Self.name =Name6Self. Testfunc ()78Def Testfunc (self):  9  ' do Base testfunc ' do Son testfunc "14 15 sonobj = Son (            
Inherit Code

After reading the code, we know the true meaning of the object and self, and now recall the order of inheritance:

Looking at the code above, let's guess what the console will print after execution. The answer is that the Base2 method is printed, and the reason is simple: Although the same TestFunc method is found in all three classes, because the computer is looking for methods, the order in which it follows is: Base2,son,base, so it will first find the Base2 class, And in this class there is just one way to find it, and it is carried out with joy!

1 #_*_coding:utf-8_*_2 __author__='Eva_j'3 classBase (object):4     defTestfunc (self):5         Print 'Do Base Testfunc'6 7 classSon (Base):8     def __init__(self,name):9Self.name =nameTen Self . Testfunc () One  A     defTestfunc (self): -         Print 'Do Son Testfunc' -  the classBase2 (object): -     defTestfunc (self): -         Print 'Do Base2 Testfunc' -  + classgrandson (Base2,son): -     Pass +  A #sonobj = Son (' sonobj ') atSonobj = Grandson ('Sonobj')
inherit2 Code

Draw a simple class diagram

  Just now we've just written a small program to illustrate the connection between classes of inheritance and object invocation methods, but if we want to hold on to a complex source logic of inheritance, we need class diagrams to help! If you think I'm going to teach you to draw a class diagram, that's a big mistake ... Lazy people with severe disease are too lazy to draw that kind of things ... Hey, look at the picture First!

See the source code in the control class diagram

Based on the diagram above, we get the related classes of threadingtcpserver and figure out the inheritance relationships and methods, and then we'll look at the process of code execution against this simple class diagram:

Initialize related procedures:

 

Execute the relevant code for Serve_forever:

That is, we follow the path of the call to find, each time we see a call against the above simple class diagram, see if there is no Name method, if there is, to find the nearest method and look inside the content, and so on: In this way, you will feel all the code in a file, Mother no longer have to worry about I can not understand the source, ha!!! Of course, this method of comparison cottage, their hearts know good, do not tell others you are in fact so read the source of ~ ~ ~

Python new skills get--look! Source! Code!

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.