Interpreting Socketserver source code in Python

Source: Internet
Author: User

Before looking at Socketserver source code, look at an example:

classBase (object):def __init__(self, name): Self.name=name self. Testfunc ()defTestfunc (self):Print('Do Base Testfunc')classSon (Base):defTestfunc (self):Print('Do Son Testfunc') Sonobj= Son ('Sonobj')
View Code

classBase (object):defTestfunc (self):Print('Do Base Testfunc')classSon (Base):def __init__(self, name): Self.name=name self. Testfunc ()defTestfunc (self):Print('Do Son Testfunc') Sonobj= Son ('Sonobj')
View Code

This is a simple class inheritance, we can see the parent class base and subclass son, each of them have a testfunc method, when we instantiate the subclass 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'll tell you how to do it in a subclass!

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.

After reading the code, we know the real meaning of the object and self, and now look at the order of inheritance:

classBase (object):defTestfunc (self):Print 'Do Base Testfunc'classSon (Base):def __init__(self,name): Self.name=name self. Testfunc ()defTestfunc (self):Print 'Do Son Testfunc'classBase2 (object):defTestfunc (self):Print 'Do Base2 Testfunc'classgrandson (Base2,son):Pass#sonobj = Son (' sonobj ')Sonobj = Grandson ('Sonobj')
View Code

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's just the way it's going to find it, and it's going to carry it!

To better understand the relationship between the inheritance between classes and the method of object invocation, we need to draw a simple class diagram:

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

Interpreting Socketserver source code in Python

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.