Detailed analysis on the usage of bind and unbound class methods in Python

Source: Internet
Author: User
This article mainly introduces the usage of bind and unbound class methods in Python, and analyzes the usage tips of unbound class methods and bound instance methods in combination with the instance form, you can refer to the following example to describe how to bind and unbind class methods in Python. We will share this with you for your reference. The details are as follows:

Like a function, class methods in Python are also an object. Since methods can be accessed through instances or classes, there are two styles in Python:

Unbound class method: no self

A class is used to reference a method to return an unbound method object. To call it, you must explicitly provide an instance as the first parameter.

Bound instance method: self

Returns a bound method object through the instance access method. Python automatically binds a method to an instance, so we do not need to pass another instance parameter when calling it.

Both methods are objects, which can be passed and saved to the list for waiting. Both require an instance as the first parameter (contains a self value), but Python automatically provides an instance when calling a binding method through an instance. For example, run the following code:

Class Test: def func (self, message): print messageobject1 = Test () x = object1.funcx ('bind method object, instance is implicit ') t = Test. funct (object1, 'method object not bound, need to pass an instance') # t ('method object not bound, need to pass an instance') # Incorrect call

Object1 = Test () generates an instance, object1.func returns a binding method, and binds object1 and func of the instance.

Test. func uses classes to reference methods. we get an unbound method object. To call it, you have to pass an instance parameter, such as t (object1, 'unbound method object, need to pass an instance ').

Most of the time, we call methods directly, so we generally do not notice the method object. However, if you start to write code for a common call object, you need to pay special attention to the unbinding method. they need to pass an instance parameter.

For more details about how to bind and unbound class methods in Python, refer to PHP Chinese network!

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.