Python's static methods and class member methods

Source: Internet
Author: User

Python's level is currently in the active phase, usually written scripts used by the Python writing is also relatively simple, did not write a slightly larger project. There is a lack of understanding of how classes in Python are being coupled between classes, and how the classes are decoupled throughout the project. Plan to read the Python code written by someone else to learn about Python's application in engineering and improve its skill level. The selected python code is the Python crawler code, the GitHub address. This code just fits the level code that jumps out of my comfort zone, so it suits my current level of learning.

After Python2.4, the main use of adorners is to implement static methods and class methods.
The adorner uses the @ operator, as the following example:

Class Example:
Val1 = "Value 1"
def __init__ (self):
Self.val2 = "Value 2"

@staticmethod
Def staticmd ():
Print ("static method, cannot 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 () #实例调用静态方法, cannot access instance variable val1 and Val2
EXAMPLE.CLASSMD () #实例调用类方法, output Result: Class method, Class: <class ' __main__. Example ' >,val1:value 1, unable to access Val2 value
EXAMPLE.CLASSMD () #类调用类方法, output Result: Class method, Class: <class ' __main__. Example ' >,val1:value 1, unable to access Val2 value
Example.val1 = "The instance value1 changed"
EXAMPLE.CLASSMD () #类调用类方法, output Result: Class method, Class: <class ' __main__. Example ' >,val1:value 1, unable to access Val2 value
Example.val1 = "The Class value2 changed"
EXAMPLE.CLASSMD () #类调用类方法, output Result: Class method, Class: <class ' __main__. Example ' >,val1:the class value2 changed, cannot access Val2 value
EXAMPLE.CLASSMD () #类调用类方法, output Result: Class method, Class: <class ' __main__. Example ' >,val1:the class value2 changed, cannot access Val2 value
It is believed that the difference between static and class methods can be clearly distinguished from the above example.

The first is the difference in the grammar above:

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 are harmless to access instance variables and class variables, class member methods cannot access instance variables but can access class variables
Differences in use:
Because the static method cannot access the Class property, the instance property, which is equivalent to a relatively independent method, does not actually have anything to do with the class. In this way, a static method is a function in the scope of a class.

Finally, how to use the static method and the class method in the actual project, this waits for some time to have the deeper experience and then communicates with everybody.

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.