function call bindings in Python, static methods, and class methods

Source: Internet
Author: User

In C + + classes, there are two kinds of functions: ordinary member functions and static member functions, the difference being that member functions are called through class instances, and static member functions are called through the class name. In essence, the member function will pass the this pointer as the first argument by default when called, and the static member function does not need to bind the this pointer.

In Python's class design, you can say that this implicit behavior of C + + is explicitly expressed.

Class Test (object):    def __init__ (self):        pass    def  func1 (self):        pass    @classmethod    def Func2 (CLS):        pass    @staticmethod    def func3 ();        Pass

A normal instance member function binds a self parameter (that is, a reference to the instance that invokes it) when called.

Class method binds the class name

Static methods are completely unbound

Note that the definition of static methods and class methods requires the use of function modifiers as above. Of course you may also see this practice in the old Code (which is equivalent):

Class Test (object):    def __init__ (self):        pass    def  func1 (self):        pass    def func2 (CLS):        Pass    Func2 = Classmethod (FUNC2)    def func3 ():        pass    func3 = Staticmethod (func3)

function call bindings in Python, static methods, and class methods

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.