Details about static and class member instances (python)

Source: Internet
Author: User
This article mainly introduces information about static methods and class members in Python. the sample code is used to give you a detailed summary of the differences between the two in terms of syntax and usage, if you need it, you can refer to it for reference. let's take a look at it. This article mainly introduces information about static methods and class members in Python. the sample code is used to give you a detailed summary of the differences between the two in terms of syntax and usage, if you need it, you can refer to it for reference. let's take a look at it.

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:
 
  
, Val1: Value 1, cannot access val2 Value Example. classmd () # Class Call class method, output result: Class method, class:
  
   
, Val1: Value 1, cannot access val2 Value example. val1 = "The instance value1 changed" example. classmd () # Class Call class method, output result: Class method, class:
   
    
, Val1: Value 1, cannot access val2 Value Example. val1 = "The class value2 changed" example. classmd () # class Call class method, output result: class method, class:
    
     
, Val1: The class value2 changed, cannot access val2 value Example. classmd () # class Call class method, output result: class method, class:
     
      
, Val1: The class value2 changed, The val2 value cannot be accessed
     
    
   
  
 

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.

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.