Explain static methods and class member methods in Python

  • 2020-05-26 09:34:03
  • OfStack

preface

Because the level of Python is in the usable stage at present, Python is relatively easy to write in the script that I usually write, and I haven't written any project that is a little larger than 1. There is a lack of understanding of the classes in Python, the organizational relationships between the classes, and how the classes are coupled across the project. I plan to read the Python code written by others to learn the application of Python in engineering and improve my technical level. The selected Python code is Python crawler code and github address. This code happens to be at a level that jumps out of my comfort zone, so it's a good fit for my current level of learning.

After Python 2.4, the main use of decorators is to implement static methods and class methods.

Decorator USES the @ operator, as shown below:


class Example:
 val1 = "Value 1"
 def __init__(self):
 self.val2 = "Value 2"

 @staticmethod
 def staticmd():
 print(" Static method, not accessible Value1 and Value2")

 @classmethod
 def classmd(cls):
 print(' Class method, class: '+str(cls)+",val1:"+cls.val1+", Don't have access to val2 The value of the ")


example = Example()
example.staticmd() # Instance calls static methods and cannot access instance variables val1 and val2
example.classmd() # Instance calls the class method, outputs the result: class method, class: <class '__main__.Example'>,val1:Value 1, Don't have access to val2 The value of the 
Example.classmd() # Class calls class method, output result: class method, class: <class '__main__.Example'>,val1:Value 1, Don't have access to val2 The value of the 
example.val1 = "The instance value1 changed"
example.classmd() # Class calls class method, output result: class method, class: <class '__main__.Example'>,val1:Value 1, Don't have access to val2 The value of the 
Example.val1 = "The class value2 changed"
example.classmd() # Class calls class method, output result: class method, class: <class '__main__.Example'>,val1:The class value2 changed, Don't have access to val2 The value of the 
Example.classmd() # Class calls class method, output result: class method, class: <class '__main__.Example'>,val1:The class value2 changed, Don't have access to val2 The value of the 

I believe that from the example above, the difference between a static method and a class method is obvious.

First, the difference in grammar:

Static methods do not need to pass in self parameters; class member methods need to pass in cls parameters that represent their class. Static methods have no problem accessing instance variables or class variables. Class member methods cannot access instance variables but can access class variables

Differences in use:

Since static methods cannot access class properties, instance properties are equivalent to a relatively independent method, which has nothing to do with the class. In this sense, static methods are functions in the scope of a class.

conclusion

Ok, the above is all the content of this article, finally, how to use static methods and class methods in the actual project, this wait 1 period of time have a deeper understanding and then communicate with you. I hope the content of this article can bring you a definite help in your study or work.


Related articles: