Python static methods and class member methods can be accessed by classes or instances. The two concepts are not easy to clarify, but there are still differences:
1) The self parameter is not required for static methods, and the class member method must be passed in the cls parameter representing the class;
2) from 1st, the static method cannot access the instance variables, and the class member methods cannot access the instance variables, but the class variables can be used;
3) static methods are a bit like Function Tool libraries, while class member methods are closer to static methods similar to Java object-oriented concepts.
Static methods and class methods
I. Using staticmethod and classmethod type object packaging before Python 2.3
The example is as follows (note the description in print ):
Class MyClass:
Val1 = 'value 1'
Def _ init _ (self ):
Self. val2 = 'value 2'
Def staticmd ():
Print 'static method, unable to access val1 and val2'
Smd = staticmethod (staticmd)
Def classmd (cls ):
Print 'class method, class: '+ str (cls) +', val1: '+ cls. val1 +', cannot access val2 value'
Cmd = classmethod (classmd)
Run:
>>> Mc = MyClass ()
>>> Mc. smd ()
>>> Mc. cmd ()
>>> MyClass. smd ()
>>> MyClass. cmd ()
II. Implementation with decorators in Python 2.4 and later
The decorator uses the @ operator, for example:
Class MyClass:
Val1 = 'value 1'
Def _ init _ (self ):
Self. val2 = 'value 2'
@ Staticmethod
Def staticmd ():
Print 'static method, unable to access val1 and val2'
@ Classmethod
Def classmd (cls ):
Print 'class method, class: '+ str (cls) +', val1: '+ cls. val1 +', cannot access val2 value'
No matter which of the above two methods, the execution is the same. The result of method 2 is analyzed as follows:
Run:
>>> Mc = MyClass () # instantiate
>>> Mc. staticmd () # The instance calls the static method and cannot access the instance variables val1 and val2.
>>>
Static Method, unable to access val1 and val2
>>> Mc. classmd () # The instance calls the class method. Note that the value of val1, the variable of MyClass, is accessed here, instead of val1, the instance variable of mc after instantiation. The printer is easy to confuse here, you will understand it later. Val2 is always an instance variable, so it cannot be accessed
>>>
Class method, class :__ main _. MyClass, val1: Value 1, cannot access the Value of val2
>>> MyClass. staticmd () # The class calls the static method directly. The result is called by the same instance as the preceding one. Both class variables and instance variables cannot be accessed.
>>>
Static Method, unable to access val1 and val2
>>> MyClass. classmd () # The class calls the class method directly. The result is called by the same instance as above.
>>>
Class method, class :__ main _. MyClass, val1: Value 1, cannot access the Value of val2
>>> Mc. val1 = 'value changed '# change the Value of instance variable val1
>>> Mc. classmd () # The instance calls the class method and notices that the value of cls. val1 has not changed. Therefore, cls. val1 is the class variable val1, not the instance variable val1.
>>>
Class method, class :__ main _. MyClass, val1: Value 1, cannot access the Value of val2
>>> MyClass. classmd () # The class calls the class method directly. The result is called by the same instance as above.
>>> Www. software8.co
Class method, class :__ main _. MyClass, val1: Value 1, cannot access the Value of val2
>>> MyClass. val1 = 'class Value changed '# change the Value of the Class variable val1
>>> Mc. classmd () # The instance calls the class method and notices cls. the value of val1 has changed, so it further proves the cls. val1 is the class variable val1, not the instance variable val1
>>>
Class method, Class :__ main _. MyClass, val1: Class Value changed, cannot access val2 Value
>>> MyClass. classmd () # The class calls the class method directly. The result is called by the same instance as above.
>>>
Class method, Class :__ main _. MyClass, val1: Class Value changed, cannot access val2 Value
Conclusion
If the execution process is too complex, remember the following two points:
Static Method: The class attributes and instance attributes cannot be merged. It is equivalent to a relatively independent method and has nothing to do with the class. In other words, it is actually a function in the scope of a class.
Class member method: You can renew class attributes and cannot access instance attributes. The above variable val1 is a class variable in the class and an instance variable in the instance, so it is easy to confuse.