Home  >  Article  >  Backend Development  >  Detailed explanation of examples of static and class members (python)

Detailed explanation of examples of static and class members (python)

Y2J
Y2JOriginal
2017-05-06 14:30:061344browse

This article mainly introduces the relevant information about static methods and class members in Python. The article summarizes the differences in syntax and usage between the two in detail through example code. Friends in need can refer to it. Below Let’s take a look.

Preface

Because the level of Python is currently at the usable stage, the scripts written in daily use Python are also relatively simple. , I have never written a slightly larger project. There is still a lack of understanding of classes in Python, the organizational relationships between classes, and how classes are coupled in the entire project. I plan to read Python code written by others to learn the application of Python in engineering and improve my technical level. The selected Python code is the Python crawler code, github address. This code just meets the level of code that is out of my comfort zone, so it is very suitable for my current level to learn.

After Python 2.4, decorators are mainly used to implement static methods and class methods.

The decorator uses @operator, the example is as follows:

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

 @staticmethod
 def staticmd():
 print("静态方法,无法访问Value1和Value2")

 @classmethod
 def classmd(cls):
 print('类方法,类:'+str(cls)+",val1:"+cls.val1+",无法访问val2的值")


example = Example()
example.staticmd() #实例调用静态方法,无法访问实例变量val1和val2
example.classmd() #实例调用类方法,输出结果:类方法,类:,val1:Value 1,无法访问val2的值
Example.classmd() #类调用类方法,输出结果:类方法,类:,val1:Value 1,无法访问val2的值
example.val1 = "The instance value1 changed"
example.classmd() #类调用类方法,输出结果:类方法,类:,val1:Value 1,无法访问val2的值
Example.val1 = "The class value2 changed"
example.classmd() #类调用类方法,输出结果:类方法,类:,val1:The class value2 changed,无法访问val2的值
Example.classmd() #类调用类方法,输出结果:类方法,类:,val1:The class value2 changed,无法访问val2的值

I believe from the above example , you can clearly distinguish the difference between static methods and class methods.

The first is the difference in syntax:

  • Static methods do not need to pass in the self parameter, classMember methodsYou need to pass in the cls parameter representing this class;

  • Static methods can access instance variables and class variables. Class member methods cannot access instance variables but can access class variables

Differences in use:

Because static methods cannot access class attributes, instance attributes are equivalent to a relative Independent methods have nothing to do with classes. In this way, static methods are just functions in the scope of the class.

【Related recommendations】

1. Python Free Video Tutorial

2. Python Learning Manual

3. Python object-oriented video tutorial

The above is the detailed content of Detailed explanation of examples of static and class members (python). For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn