First of all, this is a classic question.
Let's first make a comparison:
The first parameter of the Classmethod is the CLS, which is called to pass the class to the
This means that we can use the properties of the class in Classmethod instead of the properties of the instance of the class (explicit creation is available)
Staticmethod, there are no arguments at the time of invocation, that is, we do not pass in things (classes, instances of classes).
This means that we cannot get an instance of the class in Staticmethod (explicit creation is available)
And when we call the normal method of the class, we 're going to pass the self in.
This means that in this common method, we can only use the property method of the instance of the Class (self)
As for how to call it? See below:
class A (object): Bar = 1 def foo (self): print
" foo '
@classmethod def Class_foo (CLS): print'class_foo ' print cls.bar # equals call to A.bar cls (). Foo () # equals call A (). Foo () a.static_foo () A.class_foo ( )
Output:
Static_foo, 1,foo
Class_foo,1,foo
Glance
Classmethod and Staticmethod in Python