2015/9/21 Python Basics (17): Bindings and Method invocations

Source: Internet
Author: User
Tags instance method

Bindings and Method invocations
Now we need to re-articulate the concept of binding in Python, which is primarily associated with method invocations.
A method is a function defined inside a class, which means that the method is a class property rather than an instance property.
Second, a method can be called only if the class it belongs to has an instance. When there is an instance, the method is considered bound to that instance, and when there is no instance, the method is unbound.
Any one of the method definitions has a parameter that is the variable self. It represents an instance object that calls this method.

Core NOTES:
The self variable is used to refer to the instance that the method is bound to in the class instance method. An instance of a method is always passed as the first parameter in any method call, and self represents an instance of the method. You must put self in the method declaration, and if you do not use self in your method, consider creating a regular function, unless there is a special reason. After all, no instance is used in the method code, and its functionality is not associated with the class, which makes it look like a regular function.

Calling binding methods
An instance can invoke a bound method, which, when invoked, does not need to explicitly pass in to self, only requires passing in other parameters, because when we follow the declaration, self must be paid as the first parameter.

Calling a non-binding method
Calling a non-binding method is not often used. The main scenario for this approach is that you are deriving a subclass, and you want to overwrite the parent class's method, which you need to call the constructor that you want to overwrite in the parent class.

class empladdrbookentry (addrbookentry): ' Employee Address book Entry Class ' def __init__ (self, NM, ph, ph) addrbookentry. __init__  == em  

We refactored the constructor of the subclass, but wanted to reuse the code as much as possible rather than copy and paste the code, so the constructor of the parent class was called.
When a empladdrbookentry is instantiated, call __init__ (), although we do not have an instance of Addrbookentry, but can still call such a method.
This is the best place to call a non-binding method. We call the parent class constructor in the subclass constructor and explicitly pass the self parameter required by the parent class constructor. The first line of __init__ () in a subclass is a call to the parent class __init__ (). We call it through the parent class name, and once the call returns, we define the customizations that are used only in subclasses.


Static Methods and Class methods
A static method is only a function in a class (no instance is required), and the usual method requires an instance (self) as the first parameter, which is automatically passed to the method for the bound method call. For a class method, a class is required instead of an instance as the first argument, which is passed to the method by the interpreter, and the class does not need to be specifically named, like self, but many people use the CLS as the variable name.

Staticmethod () and Classmethod () built-in functions
Let's create a static method and a class method:

class Teststaticmethod (object): def foo (): Print ' calling static method foo () '>>> >>> a = Teststaticmethod ()>>>"< pyshell#15>" in <module>A.foo () Typeerror:foo () takes no arguments (1 Given

When we call this method, the interpreter has an error and displays a general method declaration that requires self. So what should we do?

class Teststaticmethod: def foo (): Print ' calling static method foo () '  = staticmethod (foo)>>> a = Teststaticmethod ()>>> A.foo () calling static method foo ()>>> Teststaticmethod.foo () calling static method foo ()

We use the Staticmethod () built-in function to access the method normally through classes or instances.
Similarly, class methods require this definition:

>>>classTestclassmethod (object):deffoo (CLS):Print 'Calling class Method Foo ()'      Print 'foo () is part of class:'Cls.__name__Foo=Classmethod (foo)>>>Testclassmethod.foo () callingclassmethod foo () foo () isPart ofclass: Testclassmethod>>> B =Testclassmethod ()>>>B.foo () callingclassmethod foo () foo () isPart ofclass: Testclassmethod

The CLS is used here as the first parameter of the class method, which is not required, of course.

Using function modifiers
It's annoying to see meaningless syntax like Foo=staticmethod (foo). It is only temporary and needs to be addressed by the community for these semantics.
We can use the function modifier on this function object and use it to organize the syntax. As above, we can use this write to prevent re-assignment:

classTeststaticmethod (object): @staticmethoddeffoo ():Print 'calling static method foo ()'classTestclassmethod (object): @classmethoddeffoo (CLS):Print 'Calling class Method Foo ()'Print'foo () is part of class:'Cls.__name__ 

2015/9/21 Python Basics (17): Bindings and Method invocations

Related Article

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.