Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does python modify the value of a class variable

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces the relevant knowledge of "how python modifies the value of class variables". The editor shows you the operation process through an actual case. The operation method is simple and fast, and it is practical. I hope this article "how to modify the value of class variables by python" can help you solve the problem.

It is impossible to modify the value of the class variable through the class object, but the essence is to add two instance variables, name and age, to the class object. It does not affect other instantiated objects of the class, let alone class variables with the same name.

Unlike class variables, instance variables can only be accessed through the class object name, not through the class name.

Changing the value of a class variable through cls and class name modifies the class variable with the same name, affecting all instantiated objects

Class Person: name='jerry' age='20' def _ init__ (self, name, age): self.name = name self.age = age print ("constructor", name, age) @ classmethod def A (cls): # cls () is equivalent to class Person (), # p=cls ("Tom") whose class variable cannot be modified through class object p 18) print ('cls:', cls, p) return p @ classmethod def B (cls): # class method cls can directly call class variable print ("B result:", cls.name, cls.age) @ classmethod def C (cls): # class method cls can directly call class variable # cls can modify the value of class variable like class name Will affect all instantiated objects cls.name= "Cao Cao" cls.age=50if _ _ name__ = ='_ main__': Person.B () Person.C () print ("C method modified The value of the class variable: ", Person.name, Person.age) # you can use the class name to directly call the class method person = Person.A () print (person.name, person.age) print (" value of the final class variable: ", Person.name, Person.age) D:\ MC\ venv\ Scripts\ python.exe D:/MC/test01.py

B results:

Jerry 20

After the C method is modified, the value of the class variable: Cao Cao 50

Constructor Tom 18

Cls: Tom 18

The value of the final class variable:

Cao Cao 50

Process finished with exit code 0

I instantiated the class Man twice, and each instance has a different id. This depends on the class method: first, describe the class method with @ classmethod, and then use "cls" to represent the class. The processing of class attributes by class methods is memorable.

It is important to note that the variables handled by class methods must be class variables. Because you can't use self to address instance variables in class methods, you need to put class variables at the top of the description, as shown in "id=0" above. Class variables are accessible by self, so after the class variables are defined, there is no need to describe the class variables again in the _ init_ function. Therefore, self.id is not necessarily required in the above code.

For students who need to change the value of the class variable (each time the id_number (cls) method is called, the value of the class variable id will change), you can effectively use this technique to load the model. For example, encapsulate the model into a class, initialize the network and load the model with class methods, so that after each load, you can always use the model.

Class Man: id = 0 # class variable def _ init__ (self, name): self.name = name self.id = self.id_number () @ classmethod def id_number (cls): # class methods can change class attributes, and the processing of class attributes is memorable. The variables handled by # class methods must be class variables. Because you can't use self to address instance variables in class methods, # you need to put the class variables at the top of the description, as shown in the "id=0" above. Cls.id + = 1 cls.ww=2 return cls.id, cls.ww a = Man ('A') print (a.id) b = Man ('B') print (b.id) c=Man.id_number () print (c) D:\ MC\ venv\ Scripts\ python.exe D:/MC/test2.py (1,2) (2,2) (3,2) Process finished with exit code 0 on "how python modifies the value of class variables" ends here Thank you for your reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report