Python Object-oriented 3

Source: Internet
Author: User

Next, we go on to Python's object-oriented, and in our last blog, we introduced the properties of classes and objects in detail, and today, let's look at the object-oriented approach in detail!

 1. Defining instance Methods

The private property of an instance is a property that begins with __ and cannot be accessed externally, but it is accessible from within the class, but you can define the instance's methods, in addition to the properties of the instance.

In fact, the method of an instance is to define a function in the class, and its first argument is always self, pointing to the instance itself that invokes the method, with the other parameters consistent with the normal parameters.

  

Get_name () is an instance method whose first argument is self, and this parameter does not need to show the incoming

The invocation instance method must be called on the instance.

All instance properties can be accessed within an instance method, so that if external access to private properties is required, it can be obtained by means of a method call, which encapsulates the form to protect internal data consistency and simplifies the difficulty of external invocation. The property can be accessed by means of a method to avoid modification of the attribute.

 2. The method is actually a property

The instance method we define in class is also an attribute, which is actually a function object:

  

That is, P1.get_grade returns a function object, but this function is a function bound to an instance, and P1.get_grade () is the invocation of the method.

Because the method is also a property, it can also be dynamically added to the instance, just need to use types. The Methodtype () method turns a function into a method,

  

Here, let the function and the instance bind.

Python Object-oriented 3

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.