The two methods of writing class with old Ziko python

Source: Internet
Author: User
Data Flow Process

In addition to being able to write this function in a class, you can also write other functions in the class, extending the previous example:

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

#运行结果

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

Crossing may have noticed that this code has a function author (self) More than the previous one, which we'll break down in detail later. Let's start by looking at how the data flows through this code. In order to be able to clear, draw a picture, so-called a picture wins the thousand words, has the picture to have the truth.

Define the class person, and then create the instance Laoqi=person ("Laoqi"), crossing note the direction of the arrows on the view. Laoqi This instance corresponds to self in the person class, both of which refer to the instance object (many times simplifying it as an instance object). "Laoqi" is a specific data, through the constructor of the name parameter, passed to the property of the instance Self.name, in the class person in another method author the parameter list of the first is self, indicating to undertake the self object, return Self.name, it is through the self object inside the class, the data of its property self.name is conducted as author.

When running Laoqi.author (), is to 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 parameter is not written here, This tells the process is the Python automatically completes, does not need us to worry about), the author method returns the Laoqi instance the attribute, because has already completed the Laoqi and self the correspondence process, therefore this time author inside Self is Laoqi, Natural Self.name=laoqi.name.

Crossing 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 possible to write Laoqi.name directly?

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 #返回

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, the result of the instance output of info is the same as the result of the Laoqi instance output. It turns out that when the info instance is called, it runs to author () and returns LAOQI.NAME. So, be sure to use the self instance here. When a different instance is called, Self automatically matches, and of course the matching process is Python, and we still don't have to worry about it.

OK, the data flow process, crossing understand it? The following steps into the method of writing

Why should I use the method

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

The code is 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 a function method, which is like the previously learned function, what to write in the function, and no special provisions. However, the first argument of this function must be self, or it can have no other arguments, but self must be written and is the first one. The function of the self parameter has been mentioned before.

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

The function inside the class is called a method.

The reason why use the method, also uses the class, also uses the function reason, all is in order to reduce the code redundancy, enhances the code reusability, this is also the OOP reason.

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

How to write and manipulate

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

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

#运行结果

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

In this code, the author method adds a parameter address, when calling this method: Laoqi.author ("China"), to assign a value to this parameter, crossing special attention, in the class, this method shows that there are two parameters (self,address), But in the call, the first parameter is automatically the instance Laoqi with it, do not need to be explicit assignment, can be understood as implicit completion (in fact, can also be laoqi as a hidden subject, secretly more self hook hooked).

Through the above, crossing can try the class. Note that it is important to have a clear flow of data to the class.

  • 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.