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