The use and distinction of the Python Staticmethod,classmethod method and the role of the property decorator

Source: Internet
Author: User
Tags instance method

classKls (object):def __init__(self, data): Self.data=DatadefPrintd (self):Print(self.data) @staticmethoddefSmethod (*Arg):Print('Static:', Arg) @classmethoddefCmethod (*Arg):Print('Class:', Arg)>>> ik = Kls (23)>>>Ik.printd ()23>>>Ik.smethod () Static: ()>>>Ik.cmethod () Class: (<class '__main__. Kls'>,)>>>Kls.printd () Typeerror:unbound method Printd () must be called with Kls instance as first argument (got nothing instea D)>>>Kls.smethod () Static: ()>>>Kls.cmethod () Class: (<class '__main__. Kls',)
In the above example Printd is an instance method, Smethod is a static method, Cmethod is a class method, then what is the difference between them?
Instance methods pass the instance itself as the first argument to an instance method
LK.PRINTD () will pass LK to the Printd method
Class method
Kls.cmethod () passes Kls to the Cmethod method
When this method is called, we take the class as the first argument, not the instance of that class (as we usually do). This means that you can use the class and its properties within the method, rather than a specific instance
Static methods do not pass in classes or instances to class methods
When this method is called, we do not pass an instance of the class to it (as we usually do). This means that you can place a function in a class, but you cannot access an instance of that class (which is useful when your method does not use an instance)
Typically, an instance method is used, and after the class instance is initialized, the instance can invoke an instance method in the class.
Class methods are provided for internal use within a class, and do not require initialization of class instances.
Static methods in methods that do not use instances are used
Example Link: https://www.zhihu.com/question/20021164/answer/18224953
Python's built-in @property decorator is responsible for turning a method into a property call.

The use and distinction of the Python Staticmethod,classmethod method and the role of the property decorator

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.