Learn details such as Python with lazy

Source: Internet
Author: User
There have been a lot of descriptions about the class, but the topic is far from over. However, for beginners, it is already an entry. in future practices, you still need to understand and understand it. I have discussed OOP-related issues with a few friends over the past few days. they are: Ling Hu, Frank, Jin Jian, Xiao Feng.

We have different opinions on OOP. The so-called engineering and academic views are inconsistent. From the application perspective, the engineering viewpoint is worth recommending, that is, you don't have to worry too much about how internal work is done, as long as you can solve the current problem. However, for learners, if they only stay at the engineering level (note that these friends are engineering heroes, they are not simply usable, in fact, it is a higher level of "no action, no action, no action"), and the learners may feel a little inattentive. Therefore, learners, especially beginners, need to know some internal reasons, but do not forget the purpose of the application to study the internal reasons. It seems that coordination between the two is still difficult. Don't worry. as the practice goes deeper, you will get started.

Below I will list some common causes for using OOP based on "OOP in the Master Eye" in "Learning Python" by MARK Lutz.
• Code reuse. This is simple (and the main reason for using OOP ). By supporting inheritance, classes can be customized for programming, rather than starting a project from scratch each time.
• Encapsulation. Encapsulate the implementation details of an object interface to isolate the impact of code modifications on users.
• Structure. Class provides a new local scope to minimize variable name conflicts. They also provide a natural place to write and find implementation code and manage object states.
• Maintainability. Class naturally promotes code decomposition, which reduces redundancy. Reuse the structure and code of the support class, so that you only need to modify one copy of the code at a time.
• Consistency. Class and inheritance can implement common interfaces. In this way, the code not only has a unified appearance and perception, but also simplifies code debugging, understanding, and maintenance.
• Polymorphism. Polymorphism makes code more flexible and has extensive applicability. (This seems to be the property of OOP, not the reason for using it)

In any case, class is a very important thing. when you are studying, you must use more.

In addition, for python2, there is another thing called "new-style", which corresponds to the class mentioned above, the class mentioned above is called the "classic" class. However, for Python3, there is no such difference, and the two are integrated. In Python2, the two are different. In the basic section of this tutorial, we still do not teach new types of questions. if you are interested, you can search for related information on GOOGLE, or go deeper with this course, to the next stage.

Binding and unbinding methods

Do you still remember that when learning class methods, we mentioned that class methods are functions, but the performance of these functions is a little different from that of the previous functions, such as self. Of course, it is not necessary either. the following view will show that there is no self. Since methods and functions are essentially functions, it is clear when learning functions: functions are objects, so class methods are also objects. As I said just now, some of the class methods can have self, and some can have no. In order to make the difference, we further define the following:
• No bound class method object: no self
• Bind instance method object: self

Call the bound instance method object

The code is as follows:


>>> Class MyClass:
... Def foo (self, text ):
... Print text
...

The following method can be used to call the instance method:

The code is as follows:


>>> A = MyClass () # create a class instance
>>> A. foo ('qiwsir. github. io') # Call the instance method
Qiwsir. github. io
>>> A. foo
>

When this instance method is called, its data transmission process has a figure in writing the second method. The figure shows that in the above call method, in fact, the instance name a has been passed to self, which is to call the bound instance method object, which has self.

The above call process can also be implemented as follows:

The code is as follows:


>>> A = MyClass ()
>>> X = a. foo # bind instance a and method function foo
>>> X
>
>>> X ("qiwsir. github. io ")
Qiwsir. github. io

In the above call, it is actually equivalent to the decomposition action of the previous call process. That is, bind instance a and method function foo first, and then assign the value to x. At this time, x is equivalent to a simple function. you can pass in parameters through the above method. Here, the instance and method function are bound by the vertex calculation (object. method_function)

Call a method object without binding class

The so-called class method object is to obtain the method function (ClassName. method_function) instead of using an instance, but using the class for vertex calculation)

The code is as follows:


>>> A = MyClass ()
>>> Y = MyClass. foo # Class call is not used here
>>> Y

In this way, no bound method object is obtained. However, the instance must be input as the first parameter during the call, as shown below:

The code is as follows:


>>> Y (a, "qiwsir. github. io ")
Qiwsir. github. io

Otherwise, an error is returned. Please pay special attention to the error message.

The code is as follows:


>>> Y ("qiwsir. github. io ")
Traceback (most recent call last ):
File" ", Line 1, in
TypeError: unbound method foo () must be called with MyClass instance as first argument (got str instance instead)
>>>

In programming practice, it seems that you can use the instance method to call more.

Document string

When writing a program, you must write necessary text descriptions. there is no other reason, unless your code is easy to understand, in particular, it is easy for anyone to understand the naming of various variables, functions, and classes. otherwise, text descriptions are indispensable.

Write the document string description at the beginning of a function, class, or file. Generally, the double quotation marks are used. The biggest advantage of this write is that it can be viewed using the help () function.

The code is as follows:


"This is python lesson """

Def start_func (arg ):
"This is a function ."""
Pass

Class MyClass:
"Thi is my class ."""
Def my_method (self, arg ):
"This is my method ."""
Pass

Such a document is required.

Of course, in programming, there are many places to use the "#" symbol for comments. Generally, this is used to annotate the local part.

The class is not over yet. However, this lecture is about this class. More practices are required.

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.