Python method call

Methods of classes in Python come in two forms:

1. Binding methods and non-binding methods

        The concept of binding is mainly associated with method invocation, a method is a function defined inside a class (which means that a method is a class attribute rather than an instance attribute), a method can only be called if the class to which it belongs has an instance, and when there is a instance, the method is considered to be bound to that instance. The first parameter in any method definition is the self variable, which represents the instance object on which the method is called.

class Person():
    def __init__(self,name,age):
        self.name = name
        self.age  = age
    def talk(self):
        print "talk calling"

p = Person('rhx',25)
print p.talk
<bound method Person.talk of <__main__.Person instance at 0x027A4DF0>>

Note that p.talk() is not called here, for another case, there is no self variable in the defined talk() function

class Person():
    def __init__(self,name,age):
        self.name = name
        self.age  = age
    def talk():
        print "talk calling"

p = Person('rhx',25)
print p.talk
<bound method Person.talk of <__main__.Person instance at 0x02814DF0>>

It is found that the instantiated object p can be called regardless of whether there is self in talk or not. This also shows that whether it is a method in a class or a function defined in a class, it is bound to the object by default. When calling a bound method in an instance, there is no need to manually pass in parameters.

class Person():
    def __init__(self,name,age):
        self.name = name
        self.age  = age
    def talk():
        print "talk calling"

p = Person('rhx',25)
print Person.talk
print p.talk
<unbound method Person.talk> #Use the class name to call, it is an unbound method
<bound method Person.talk of <__main__.Person instance at 0x02794DF0>> #Use the instantiated object to call as a bound method

For the case with self variable in talk

class Person():
    def __init__(self,name,age):
        self.name = name
        self.age  = age
    def talk(self):
        print "talk calling"

p = Person('rhx',25)
print Person.talk()
print p.talk()
TypeError: unbound method talk() must be called with Person instance as first argument (got nothing instead)

The result shows that the instantiated object of the class must be passed in when talking() is called. When the class calls the method in the class, it will not automatically pass the value. That is to say, if the function has several parameters, it must pass several parameters. parameter. If you want the result to work properly, then when the class name calls talk(), pass in all the parameters one by one. which is:

print Person.talk(p)
class Person():
    def __init__(self,name,age):
        self.name = name
        self.age  = age
    def talk():
        print "talk calling"

p = Person('rhx',25)
print Person.talk
print p.talk()
    print p.talk()
TypeError: talk() takes no arguments (1 given)

Judging from the output results, when Person calls the talk() method, it does not need to pass parameters; when the object calls talk(), because the object calls its own binding method, the instantiated object is automatically regarded as the first A parameter is passed in, so when the talk() method in the class has no parameters, and you pass another one to it, it will obviously report an error.

  From the above, we can draw the following conclusions: 

    1. All the methods and functions in the class are bound to the object for use;

    2. Binding methods have the function of automatically passing values. The value passed in is the object itself.

    3. If the class wants to call the bound method, it must follow the parameter rules of the function. If there are several parameters, several parameters must be passed.

Since the methods in the class are all bound to the object, are there any methods that are bound to the class?

2. Static methods and class methods

      Since the methods in the class are bound to the object by default, we need to take some measures to unbind the binding method in the class from the object, and then bind it to the class. The usual method requires an instance of self as the first argument, and for (bound) method calls, self is automatically passed to the method, while for class methods, the class is required instead of the instance as the first argument, which is used by the interpreter passed to the method. In python, the @classmethod method is introduced to bind the methods in the class to the class.

class Person():
    def __init__(self,name,age):
        self.name = name
        self.age  = age
    @classmethod               #talk = classmethod(talk)
    def talk(cls): #cls is the class itself
        print "talk calling"

p = Person('rhx',25)
print Person.talk()
print p.talk()

The reason why it is also feasible to instantiate an object and call a class method is that the class method also has a parameter cls. When the instantiated object is called, the instantiated object self is passed to the formal parameter cls. The methods in the class are bound to the object by default. When the object calls the bound method, the object will be automatically passed in as the first parameter; and the class is called, it must follow the one-to-one correspondence rule of function parameters. Several parameters, you must pass several parameters. If a method uses the @classmethod decorator, then the method is bound to the class. Whether it is called by an object or a class, the class will be passed in as the first parameter.

In the previous example, for talk with no arguments, the class call is

class Person():
    def __init__(self,name,age):
        self.name = name
        self.age  = age
    def talk():
        print "talk calling"

p = Person('rhx',25)
print Person.talk()
print p.talk()
    print Person.talk()
TypeError: unbound method talk() must be called with Person instance as first argument (got nothing instead)

But when passing an instantiated object p

print Person.talk(p)
There will still be problems, because the method talk has no parameters
   print Person.talk(p)
TypeError: talk() takes no arguments (1 given)

This time you need a static method

class Person():
    def __init__(self,name,age):
        self.name = name
        self.age  = age
    @staticmethod         #talk = staticmethod(talk)
    def talk(): #Cannot pass class or instance related parameters, such as cls or self, but other parameters can be passed
        print "talk calling"

p = Person('rhx',25)
print Person.talk()
print p.talk()










Guess you like

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