python-binding and unbound method popular explanation

Source: Internet
Author: User
Like a function, a class method in Python is also an object. Because methods can be accessed either through an instance or through a class, there are two styles in Python: Unbound class methods: No self Returns an unbound method object through a class-referenced method. To invoke it, you must present an instance as the first parameter. instance method of binding: There is a method object that returns a binding through the instance access method. Python automatically binds an instance to a method , so we don't have to pass an instance argument when we call it. Both methods are objects, which can be passed and stored in the list for waiting. both runs require an instance as the first parameter (妤 a self value), but Python automatically provides one when a binding method is called 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 implied ') t= Test.funct (Object1, ' unbound method object, need to pass an instance ') #t (' unbound method object, need to pass an instance ') #错误的调用 object1=test () generates an instance, Object1.func returns a bound method that binds the instance Object1 and the method func. And Test.func is using the class to refer to the method, we get an unbound method object. To invoke 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 the method directly, so we generally don't notice the method object. However, if you start writing the code for a generic calling object, you need to pay particular attention to the unbound methods, which need to pass an instance parameter

python-binding and unbound method popular explanation

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.