Describes the static methods and class member methods in Python.

Source: Internet
Author: User

Describes the static methods and class member methods in Python.

Preface

Because the level of Python has been in the usable stage, the script used in normal times is also relatively simple in writing, and has not written a slightly larger project. There is still a lack of understanding about the relationships between classes and classes in Python and the coupling between classes in the entire project. I plan to read the Python code written by others to learn Python's application in engineering and improve my technical skills. The Python code selected is the Python crawler code, github address. This code is suitable for the code that jumps out of my comfort zone, so it is suitable for my current level to learn.

After Python2.4, the decorator is used to implement static methods and class methods.

The decorator uses the @ operator, for example:

Class Example: val1 = "Value 1" def _ init _ (self): self. val2 = "Value 2" @ staticmethod def staticmd (): print ("static method, unable to access Value1 and Value2") @ classmethod def classmd (cls): print ('class method, class: '+ str (cls) + ", val1:" + cls. val1 + ", cannot access val2 value") example = Example () example. staticmd () # The instance calls the static method and cannot access the instance variables val1 and val2example. classmd () # The instance calls the class method, and the output result is: class method, class: <class '_ main __. example '>, val1: Value 1, cannot access val2 Value Example. classmd () # class calls the class method, and the output result is: class method, class: <class '_ main __. example '>, val1: Value 1, cannot access val2 Value example. val1 = "The instance value1 changed" example. classmd () # class calls the class method, and the output result is: class method, class: <class '_ main __. example '>, val1: Value 1, cannot access val2 Value Example. val1 = "The class value2 changed" example. classmd () # class calls the class method, and the output result is: class method, class: <class '_ main __. example '>, val1: The class value2 changed, The val2 value Example cannot be accessed. classmd () # class calls the class method, and the output result is: class method, class: <class '_ main __. example '>, val1: The class value2 changed, cannot access val2 Value

I believe that in the above example, we can clearly identify the differences between static methods and class methods.

First, the difference in syntax:

  • The self parameter is not required for static methods. The class member method must be passed in the cls parameter representing the class;
  • The static method does not allow access to instance variables and class variables. The class member method does not allow access to instance variables, but does allow access to class variables.

Differences:

Because static methods cannot perform class attributes, instance attributes are equivalent to a relatively independent method, which has nothing to do with classes. In this way, static methods are just functions in the scope of the class.

Summary

Well, the above is all about this article. Finally, let's talk about how to use static methods and class methods in actual projects. I hope the content of this article will help you in your study or work.

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.