With the old Ziko Python writing class two method _python

Source: Internet
Author: User

Data Flow Process

In addition to writing this function in a class, you can write another function in the class, extending the previous example:

Copy Code code 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 ()

#运行结果

Author name from Laoqi:laoqi
Author name from Info:qiwsir

Reader may have noticed that this code is a bit more than the previous one function author (self), which we do not matter, will be decomposed in detail later. First look at how the data flows through the code. In order to be able to clearly, draw a picture, the so-called a picture wins the innumerable words, has the picture to have the truth.

Define the class person, and then create the instance Laoqi=person ("Laoqi"), reader note the direction of the arrow on the watch chart. Laoqi This instance corresponds to self in the person class, all of which refer to the instance object (often simplified as an instance object). "Laoqi" is a specific data passed to the instance's property Self.name by the name parameter in the constructor, and the first in the argument list of another method author in the class person is self, which indicates that the self object is to be accepted, return Self.name, which is to pass the self object within the class, transmits its properties self.name the data as author.

When you run Laoqi.author (), you tell the above code, call the Laoqi instance object, and get the result of the author () method, Laoqi this instance is automatically told author () (note that the Self argument is not written here. This tells the process to be done automatically by Python, without our concern, the author method returns the properties of the Laoqi instance, because the corresponding process of laoqi and self has been completed before, so this time author self is Laoqi, Natural Self.name=laoqi.name.

Reader can follow me in doing an experiment, that is, in author, return laoqi.name, see what effect. Since Laoqi and self are the same instance object, is it OK to write laoqi.name directly?

Copy Code code 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 #返回

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 ()

#输出结果
Author name from Laoqi:laoqi #laoqi实例输出结果
Author name from Info:laoqi #info实例输出结果

As can be seen from the results, there is no error. However, info this instance outputs the same result as the Laoqi instance output. It turns out that when the info instance is invoked, it runs to author () and returns the Laoqi.name. Therefore, it is important to use the self instance. When you invoke a different instance, self will automatically match, of course, the matching process is Python complete, still need not worry us.

OK, the data flow process, reader understand it? Here's how to get to the writing process

Why do you use the method

In a class, you can write a function with a def statement, but usually this function looks like this:

Copy Code code as follows:

Class ClassName:
def __init__ (Self,*args):
...
Def method (Self,*args): #是一个在类里面的函数
...

In the class classname, in addition to the previous constructor with the initialization function, there is also a function method, which, like the one previously studied, has nothing to say in the function, and there is no special rule. However, the first argument of this function must be self, or it can have no other arguments, but self must be written and first. The function of this self parameter has already been mentioned before.

So it seems that the function in the class is a bit different from the previous function.

This function within the class, we call it the method.

The reason for using the method is also to use the class, but also the reason for the function, are to reduce the redundancy of the code, improve the reusability of the code, this is the reason for OOP.

How is the method reused? Look at the first piece of code, there is a author method, whether it is Laoqi or info instance, all use this method to return the name of the instance import. This is the embodiment of reuse.

How to write and operate

The process of writing a method is the same as writing a function, and it is necessary to note that self is first written in the argument list, even if there are no other parameters.

Copy Code code 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 ("a")
Print "Author name from info:", Info.author ("Suzhou")

#运行结果

Author name from Laoqi:laoqi in
Author name from Info:laoqi in Suzhou

In this code, add a parameter address to the author method, when this method is invoked: Laoqi.author ("China"), to assign a value to this parameter, reader special attention, in the class, this method shows that there are two parameters (self,address), But at the time of the call, the first parameter is to automatically match the instance Laoqi, do not need explicit assignment, can be understood as implicitly completed (in fact, you can also see Laoqi as a hidden subject, secretly more self hook up).

Through the above, reader can try the class. Remind, be sure to the class of data flow process 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.