Python3.2 Official document Translation--instance objects and method objects

Source: Internet
Author: User
Tags instance method

6.3.3Instance Object

What do we do with instance objects today? The only action available for an instance object is a property reference. There are now two legitimate property names: Data properties and methods.

Data properties are equivalent to instance variables in smallTalk , data members inC + + . Data attributes are not required to be declared. Like local coupling beam, they exist when they first assign values. For example, suppose x is an instance of the above created MyClass class, and the following code block indicates that the value will be printed . This value is no matter what the error.

X.counter = 1

While X.counter < 10:

X.counter = x.counter*2

Print (X.counter)

Del X.counter

An example of a method that is referenced by an instance property is a method. A method is a function that belongs to an object. (in python , the term method and the class instance are not unique): There are methods for other object types as well. For example,thelist object has a name of append,insert,remove,sort methods, and so on. However, in the ensuing discussion, unless specifically stated, we use the term method to use the object instance method. )

The legal method name of an instance object depends on its class. By definition, all function objects in a class define corresponding instance methods. So, in our example,x.f is a valid method reference, since myclass.f is a method, but x.i is not, because Myclass.i not. But unlike x.f and myclass.f , it is a method object, not a function object.

6.3.4Method Object

Typically, a method is called after it is bound.

X.F()

In the MyClass instance, it will return the string ' Hello World '. However, you do not need to call the method immediately: X.F is a method object that can be saved and then called later, such as:

XF = X.F

While True:

Print (XF ())

The Hello world will be printed continuously until the program terminates.

What happened in detail when the method was called? You may have noticed Although function definition f () x.f () is not called when the number of references is passed. What happened to the number? python when a function that requires a number of parameters is not invoked regardless of the number of parameters

In fact, you can test the answer: one of the characteristics of a method is that the instance object is passed to the function as the first parameter. In our example, thex.f() method is equivalent to myclass.f(x). in general, a call to include N The table is equivalent to the corresponding function, which includes a list that was created when the method object was first inserted.

Let's say you still don't have a clear picture of how the method works, and it's possible to make the truth clear. When an instance property refers to a property that is not a data attribute, its class is searchable. Suppose the name represents a legitimate function object property, and the method object is created by encapsulating the (pointer) instance object and the function object found in the abstract class. When a method object is called with a list parameter, a new table of parameters is created from the instance object and the list of references, and the function object is called with a new attribute listing.

Python3.2 Official document Translation--instance objects and method objects

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.