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