Bind unbound class methods in Python

Source: Internet
Author: User
Like a function, a class method in python is also an object that is transferred from a http://blog.sina.com.cn/s/blog_3fe961ae0100kew0.html. Because methods can be accessed through instances or through classes, there are two styles in Python: unbound class methods: no self uses a class 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, we run the following Code: Class test: def func (self, message): Print message object1 = 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 ') # Call object1 = test () Incorrectly to generate an instance. object1.func returns a binding method and binds object1 and func. 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.

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.