The second method for compiling Python

Source: Internet
Author: User
The class was created in the previous lecture, and the constructor and class instance are highlighted, especially the self, which is described a lot. When talking about the constructor, we mentioned that init () is a function, but it has a special role in the class. every class must first run it. Data transfer process

In addition to writing such a function in a class, you can also write other functions in the class to continue the example described above:

The code is as follows:


#! /Usr/bin/env python
# Coding: UTF-8

Class Person:
Def _ init _ (self, name, lang = "golang", website = "www.google.com "):
Self. name = name
Self. lang = lang
Self. website = website
Self. email = "qiwsir@gmail.com"

Def author (self ):
Return self. name

Laoqi = Person ("LaoQi ")
Info = Person ("qiwsir", lang = "python", website = "qiwsir. github. io ")

Print "Author name from laoqi:", laoqi. author ()
Print "Author name from info:", info. author ()

# Running result

Author name from laoqi: LaoQi
Author name from info: qiwsir

You may have noticed that this code is more than a function author (self) described in the previous section. we don't care about this. we will break it down in detail later. First, let's take a look at how data is transferred in this code. In order to be clear and draw a picture, the so-called figure is more than a thousand words, there is a picture with the truth.

Define the class Person, and then create the instance laoqi = Person ("LaoQi"). observe the arrows in the graph. The laoqi instance corresponds to the self in the Person class. they all reference the instance objects (which are simplified to instance objects in many cases ). "LaoQi" is a specific data that is passed to the instance property self through the name parameter in the constructor. name, the first parameter in the author parameter list of another method in the class Person is self, which indicates to undertake the self object, return self. name means to use the self object inside the class to set its attribute self. name data transmission, such as author.

When laoqi. when author () is used, it tells the above code to call the laoqi instance object and obtain the result of the author () method. the laoqi instance is automatically told author () (note, the self parameter does not need to be written here. this indicates that the process is completed automatically by python and you don't have to worry about it.) The author method returns the attribute of the laoqi instance, because the process of laoqi and self has been completed before, self in author is laoqi, natural self. name = laoqi. name.

The viewer can follow me in an experiment, that is, return laoqi. name in author to see what the effect is. Since laoqi and self are the same instance objects, can they be directly written as laoqi. name?

The code is as follows:


#! /Usr/bin/env python
# Coding: UTF-8

Class Person:
Def _ init _ (self, name, lang = "golang", website = "www.google.com "):
Self. name = name
Self. lang = lang
Self. website = website
Self. email = "qiwsir@gmail.com"

Def author (self ):
# Return self. name
Return laoqi. name # return

Laoqi = Person ("LaoQi ")
Info = Person ("qiwsir", lang = "python", website = "qiwsir. github. io ")

Print "Author name from laoqi:", laoqi. author ()
Print "Author name from info:", info. author ()

# Output result
Author name from laoqi: LaoQi # laoqi instance output result
Author name from info: LaoQi # info instance output result

The result shows that no error is reported. However, the output result of the info instance is the same as that of the laoqi instance. It turns out that when the info instance is called and runs to author (), laoqi. name is returned. Therefore, the self instance must be used here. When calling different instances, self will automatically perform matching. of course, the matching process is completed in python, so we don't have to worry about it.

OK. Does the reader understand the data transfer process? The following describes how to compile a method.

Why is the method used?

In the class, you can use the def statement to compile the function. However, this function usually looks like this:

The code is as follows:


Class ClassName:
Def _ init _ (self, * args ):
...
Def method (self, * args): # is a function in the class.
...

In the ClassName class, apart from the previous constructor with initialization functions, there is also a function method. This function is the same as the previously learned function. what should be written in the function, there are no special rules. However, the first parameter of this function must be self, or there can be no other parameter, but self must be written and the first parameter. The role of this self parameter has been mentioned earlier.

In this case, the function in the class is different from the previous function.

This function in the class is called a method.

The reason for using methods is also the reason for using classes, and also the reason for using functions. they are all used to reduce code redundancy and improve code reusability. this is also the reason for OOP.

How can methods be reused? Check the code at the beginning of this article. there is an author method in it. whether it is a laoqi or info instance, this method is used to return the instance import name. This indicates reuse.

Compiling and operation methods

The process of writing a method is the same as that of writing a function. Note that you must write self in the first parameter list, even if no other parameter exists.

The code is as follows:


#! /Usr/bin/env python
# Coding: UTF-8

Class Person:
Def _ init _ (self, name, lang = "golang", website = "www.google.com "):
Self. name = name
Self. lang = lang
Self. website = website
Self. email = "qiwsir@gmail.com"

Def author (self, address ):
# Return self. name
Return laoqi. name + "in" + address

Laoqi = Person ("LaoQi ")
Info = Person ("qiwsir", lang = "python", website = "qiwsir. github. io ")

Print "Author name from laoqi:", laoqi. author ("China ")
Print "Author name from info:", info. author ("Suzhou ")

# Running result

Author name from laoqi: LaoQi in China
Author name from info: LaoQi in Suzhou

In this code, an address parameter is added to the author method. when this method is called: laoqi. author ("China"), to assign a value to this parameter, it should be noted that in the class, this method shows that there are two parameters (self, address), but when calling, the first parameter automatically maps the instance laoqi. it can be understood as implicit without explicit assignment (in fact, it can also be regarded as a hidden entity, secretly hook up self ).

Through the above description, you can try the class. Note: the data flow process of the class must be clear.

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.