With the details of old Ziko python _python

Source: Internet
Author: User
Tags inheritance instance method

These days and several friends in various ways to discuss OOP related issues, they are: Linghutao, Frank, Jinjian, Xiao Feng

There is a different view of OOP, and there is no disagreement between the so-called engineering school and the academic school. From an application point of view, the engineering school's point of view is worthy of recommendation, that is: do not care too much about how the internal work, as long as can solve the problem at the moment. However, for learners, if only to stay at the level of engineering (especially remind, these friends are engineering faction heroes, they are not simply able to use, in fact, is a higher level of "no strokes wins"), the learner may feel a bit not thorough. Therefore, learners, especially beginners, should know some internal reasons, but do not forget the purpose of the application in order to delve into internal reasons. It seems that the coordination of the two is still a difficult thing. Do not worry, as the practice of in-depth, gradually have experience.

Below I list some common reasons for using OOP, according to Mark Lutz's "Oop in the master's Eye" in "Learning Python".
• code reuse. This is simple (and the most important reason to use OOP). By supporting inheritance, classes allow you to program by customizing instead of starting a project from scratch each time.
Package The details of the implementation are packaged after the object interface, thus isolating the effect of the modification of the code on the user.
Structure class provides a new local scope that minimizes variable name collisions. They also provide a natural place to write and find implementation code, and to manage the state of objects.
• maintainability. Class naturally facilitates the decomposition of code, which allows us to reduce redundancy. The structure of the loss support class and code reuse so that you need to modify only one copy of the code at a time.
Consistency Classes and inheritance can implement common interfaces. This code not only has a unified appearance and perception, but also simplifies the debugging, understanding and maintenance of the code.
• Polymorphism. Polymorphism makes code more flexible and has a wide range of applicability. (This seems to be the attribute of OOP, not the reason to use it)

In any case, the class is a very important thing, reader in the study time, must use more.

In addition, for Python2, there is also something called the "New Class" (New-style), which corresponds to the previous class, then the class described above is called the "classic" (Classic) class. However, for Python3, there is no such difference, the two fused. Just in the Python2, the two are different. This tutorial in the basic part, still does not teach the new class the question, if reader has the interest, may find the relevant information in the Google, also may follow this course to the next stage to study.

Binding and unbound methods

Reader remember, in learning the methods of a class, the method of a class is a function, except that the function behaves somewhat differently from the one previously learned, such as having a self. Of course, not necessarily, and below reader will see no self. Since methods and functions are essentially functions, then the part of the function that learns is already clear: the function is the object, so the class method is also the object. As just said, some of the methods of the class can have self, some can not. To make a distinction, the definition is further:
• No bound class method object: No self
• Bind instance Method object: Has self

Calling the binding instance method object

Copy Code code as follows:

>>> class MyClass:
... def foo (self,text):
... print text
...

You can invoke the instance method in the following ways

Copy Code code as follows:

>>> a = MyClass () #创建类实例
>>> a.foo (' Qiwsir.github.io ') #调用实例方法
Qiwsir.github.io
>>> A.foo
<bound method Myclass.foo of <__main__. MyClass instance at 0xb74495ac>>

In this instance method invocation, the data transfer process, in the writing class two method, has a diagram that shows that in the above invocation method, the instance name A is actually passed to self, which is called the binding instance method object, and self.

The call procedure above can also be implemented as follows:

Copy Code code as follows:

>>> a = MyClass ()
>>> x = A.foo #把实例a和方法函数foo绑定在一起
>>> x
<bound method Myclass.foo of <__main__. MyClass instance at 0xb74495ac>>
>>> x ("Qiwsir.github.io")
Qiwsir.github.io

In the above call, it actually corresponds to the decomposition action of the previous call procedure. That is, the instance a and the method function Foo are first bound together, and then assigned to X, where x is equivalent to a simple function, which can be passed in as described above. The way in which you bind instance and method functions is to use dot arithmetic (object.method_function)

Calling unbound class method objects

The so-called class method object, is not through the instance, but uses the class to carry on the point number operation to obtain the method function (classname.method_function)

Copy Code code as follows:

>>> a = MyClass ()
>>> y = Myclass.foo #这里没有用类调用
>>> y
<unbound Method myclass.foo>

Such a call, you get the unbound method object, but the call must pass the instance as the first argument, as follows

Copy Code code as follows:

>>> y (A, "Qiwsir.github.io")
Qiwsir.github.io

Otherwise, it will be an error. Please reader pay special attention to the error message

Copy Code code as follows:

>>> y ("Qiwsir.github.io")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
Typeerror:unbound Method Foo () must is called with MyClass instance as a-argument (got str instance instead)
>>>

In programming practice, it seems that more is invoked with instance methods.

Document string

When writing a program, you have to write the necessary text, no other reason, unless your code is very easy to understand, especially the various variables, functions and classes, such as naming anyone can easily understand, otherwise, the text description is indispensable.

Write a document string description at the beginning of a function, class, or file, typically using triple quotes. The greatest benefit of this writing is the ability to use the Help () function.

Copy Code code as follows:

"" "" Is Python Lesson "" "" "" "

def start_func (ARG):
' "' is a function." "" ""
Pass

Class MyClass:
"" "Thi is my class." ""
def my_method (Self,arg):
"" "" "" "" "" "" ""
Pass

Such documents are required.

Of course, in programming, there are a lot of places to use the "#" notation to do the annotation. This is generally used to annotate parts.

The class does not actually end, but this lecture is a temporary part of the class. Reader to practice more.

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.