Python 3 QuickStart modularity and class

Source: Internet
Author: User
Tags constructor inheritance

First quote the official passage

Python shows the philosophy of "Swiss Army Knife". This can be best demonstrated by the advanced and robust features of its larger package.

For example:

Xmlrpc.client and Xmlrpc.server modules make remote procedure calls easier. Although the module has such a name, use

Users do not need to have knowledge of XML or process XML. An email packet is a library that manages mail information, including MIME and other based

An information document for RFC 2822. Unlike the Smtplib and Poplib modules that actually send and receive information, the email package contains a

A complete toolset for constructing or parsing complex message structures (including attachments) and implementing Internet coding and header protocols. Xml.dom and

The Xml.sax package provides a powerful support for the popular Information Interchange format. Similarly, the CSV module supports direct in the common database format

Write. In combination, these modules and packages greatly simplify the exchange of data between Python applications and other tools. Internationalization

Supported by GetText, locale and codecs packages.

That's one of the advantages of Python.

The previous section discusses some of the basics and getting started, where you start talking about things like other languages

This time for convenience, we use vs2012, the method of installing Plug-ins in the previous document has been mentioned.

First, modular

After all, the code needs to be saved, and modular programming is also very important, so you need to save the file in a different place, especially in different folders, here to show the established file structure

Copy Code

#Projrct1. py

Import Test1

Print ("Test1.count do There")

Print (test1.__name__)

A=test1.count (1,2,3)

Print (a)

Import Newfolder1.test2

Print (' newfolder1.test2.count do there ')

Print (newfolder1.test2.__name__)

A=newfolder1.test2.count (1,2,3)

Print (a)

Print (dir (test1))

Print (dir (newfolder1.test2))

Copy Code

#test1. py

def count (a,b,c):

Return a+b-c

#test2

def count (a,b,c):

Return A+b+c

#__init__. py

Print (' Use a folder! ')

First, we can set the startup file by right-clicking the. py file.

We can use the Import keyword to invoke other module files

Explain the __init__ file, if you want to call a folder file, that folder must have such a file, before calling the file must be initialized with this file, is executed, of course, it doesn't matter if it's empty.

The built-in function dir () is used to search for the module definition by module name, which returns a list of stored string types:

The order of execution is obvious, and we can see that in the. py file, although we've only seen one function, there are actually a lot of predefined variables, such as __name__, that means the file name.

Ii. scope and Class

Python and C + + are related in class, but here's an official quote

The Python class mechanism implements classes in the language with minimal new syntax and semantics. It is a mixture of Modula-3 in C + + language. Like modules, the Python class does not have an absolute barrier between the user and the definition, but relies on the elegance of the user not to "break into the definition". On the other hand, most of the important attributes of a class are preserved intact: the class inheritance mechanism allows multiple inheritance, and derived classes can overwrite any method or class in the base class (override), and you can invoke methods of the base class using the same method name. An object can contain any number of private data.

For the time being, let's talk about the scope of things, and still use a section of the official teaching code.

Copy Code

Def scope_test ():

Def do_local ():

Spam = "Local spam"

Def do_nonlocal ():

Nonlocal spam

Spam = "nonlocal spam"

Def do_global ():

Global spam

Spam = "Global spam"

Spam = "Test spam"

Do_local ()

Print ("After local assignment:", spam)

Do_nonlocal ()

Print ("After nonlocal assignment:", spam)

Do_global ()

Print ("After global assignment:", spam)

Scope_test ()

Print ("In global scope:", spam)

Copy Code

nonlocal non-partial global global

Indeed, like C + +, design-time encapsulation is not as perfect as C #, leaving us with an opportunity to access from outside

Now let's talk about class

Inherited method Class Lei (Lei1,lei2,lei3 ...) No more mention here, you can inherit multiple classes.

One of the differences between Python2.7 and pytho3.3n is. 3.3 There is no classic class, all new classes, the original class object is not a parent class, then it is inherited from object, but in 3.3 became, in the absence of inheriting the parent class, it inherits the object class by default. Although it is a drag on efficiency, it also makes the code structure more complete.

This is somewhat related to the mainstream C #.

Let's just say it's a basic class.

Copy Code

Class Person:

Name= '

Age=0

def __init__ (SELF,NM,AG):

Self.name=nm

Self.age=ag

Print ("constructor run")

Pass

def shuchu (self):

Print (Self.name)

Print (Self.age)

Passpass

X=person (' Wangji ', 20)

X.shuchu ()

Copy Code

__init__ () is the so-called constructor, which is no longer mentioned here.

For friends who have studied C #, we can use self as this, and the first parameter to define the function is self,

Why?

The principle is the method of the object in Python, it needs to call itself first, so it counts as a parameter, here is written, the first parameter of the function we default to self can

We can try, the overload of the method is not good, but python as an object-oriented language, naturally does not lose the function of overloading this object-oriented language common to the important features .... In fact this place we have to use the default parameters to achieve this purpose. I don't know much about this place, but a method with a default parameter can really solve some of the problems associated with overloading.

All the methods in Python are essentially virtual methods, so rewriting is basically the same as other languages.

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.