Static methods and class members in Python

Source: Internet
Author: User
Objective

Because the level of Python is currently in the stage of being able to use, usually written script uses Python's writing is also relatively simple, did not write a slightly larger project. There is also a lack of understanding of how classes in Python, relationships between classes, and how classes are coupled across the entire project. I'm going to read Python code written by someone else to learn about Python's application in engineering and to improve its skills. The Python code selected is the Python crawler code, the GitHub address. This code is exactly the code that fits the level out of my comfort zone, so it's good for my current level of learning.

After Python2.4, the main use of adorners to implement static methods and class methods.

The adorner uses the @ operator, as in the following example:

Class Example:val1 = "Value 1" def __init__ (self): Self.val2 = "Value 2" @staticmethod def staticmd (): Print ("static method, unable to access VA Lue1 and Value2 ") @classmethod def CLASSMD (CLS): Print (' class method, Class: ' +str (CLS) + ', Val1:" +cls.val1+ ", inaccessible val2 value") example = Example () Example.staticmd () #实例调用静态方法, unable to access instance variables Val1 and VAL2EXAMPLE.CLASSMD () #实例调用类方法, output Result: Class method, Class: <class ' __main__. Example ' >,val1:value 1, unable to access the value of Val2 example.classmd () #类调用类方法, Output: Class method, Class: <class ' __main__. Example ' >,val1:value 1, unable to access Val2 value Example.val1 = "The instance value1 changed" Example.classmd () #类调用类方法, Output: Class method, Class: <class ' __main__. Example ' >,val1:value 1, unable to access Val2 value Example.val1 = "The class value2 Changed" EXAMPLE.CLASSMD () #类调用类方法, Output: Class method, Class: <class ' __main__. Example ' >,val1:the class value2 changed, unable to access Val2 value EXAMPLE.CLASSMD () #类调用类方法, output Result: Class method, Class: <class ' __main__. Example ' >,val1:the class value2 changed, unable to access Val2 value

It is believed from the above example that the difference between static and class methods can be clearly distinguished.

First in the syntax above the difference:

    • The static method does not need to pass in the self parameter, and the class member method needs to pass in the CLS parameter representing the class;

    • Static methods have no harm in accessing instance variables and class variables, and class member methods cannot access instance variables but can access class variables

Differences in use:

Because static methods cannot access class properties, instance properties, which are equivalent to a relatively independent method, do not actually have anything to do with classes. In this way, a static method is just a function 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.