Bound and unbound methods in python

  1. The binding method of the object

As you can see from the above figure, the same talk(),

When the instance object calls this method, the method is bound to the calling object's method. (When the instance object calls the method bound to the object, it will pass the instance object to the first parameter of the bound method)

When the class is called, talk() is a function in the class People ( unbound methods, like the static method types modified with @staticmethod analyzed in Part 3 below, are functions in the class, that is, unbound methods). method). (A function in a class, with several parameters passed several parameters).

As above, when the class People calls the function talk(), no parameters are passed, and an exception occurs. When the instance object p calls the method talk(), although no parameters are passed, the instance object is automatically passed as an actual parameter to

The first parameter self of the talk(self) method.

Note: Binding methods have the function of automatically passing parameters.

Not bound methods, such as functions in classes, static methods require several parameters to pass several parameters.

2. The binding method of the class

In python, use the @classmethod method to bind the methods in the class to the class. Take a look at the code below:

In [99]: class People:
    ...:     clsname="clsname" ...: def __init__(self,name): ...: self.name = name ...: ...: @classmethod ...: def talk(cls): ...: print(cls.clsname) ...: pass ...: ...: p = People('xiaohua') ...: print(People.talk()) ...: print(People.talk) ...: print(p.talk()) ...: print(p.talk) ...:

运行结果 clsname None <bound method type.talk of <class '__main__.People'>> clsname None <bound method type.talk of <class '__main__.People'>>

The results are as follows:

After declaring a class method using @classmethod, the method is a class method bound to the class, which can be called with only the class name. or with the instance name. 
When used, when these two methods are called, the class object will be automatically passed to the first parameter of the method.

3. The unbound method


can be seen from the above figure, whether it is to use the class name. call, or use the instance name. to call a static method, it needs to have several formal parameters, just pass a few actual parameters.

Summary
1. Unbound methods: static methods decorated with @staticmethod (as introduced in the third part), functions in the class (as introduced in the first part, by class name. Called without any decorator decoration function)
2. Binding methods: divided into methods bound to objects and methods bound to classes.
(1) A method bound to an object: a method in a class, by object name. A method called (as introduced in the first part)
(2) A method bound to a class: a method decorated with the @classmethod decorator (As introduced in the second part)


If there are any mistakes, you are welcome to point them out.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325337849&siteId=291194637