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

What is the use of class properties, class methods, and static methods in Python

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

Share

Shulou(Shulou.com)06/02 Report--

This article is about the usefulness of class properties, class methods, and static methods in Python. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Class properties and object properties

Class attributes are also called static properties (fields). General properties (properties that do not modify) can be set into class properties, for example: a person has 2 eyes (class attribute), and the eye color is blue (object attribute).

Class Eye: eyesNum=2 def _ init__ (self,color): self.color=coloreye1=Eye ('blue') eye1.eyesNum=3 # sets the number of eyes of the eye1 object print (eye1.color) print (eye1.eyesNum) print (Eye.eyesNum) # output blue#3#2

Class method:

@ classmethod

Class Test: num=100 def _ init__ (self,num): self.num=num @ classmethod # Class method def setNum (cls,num): cls.num=numtest=Test (100) print (test.num) Test.setNum (200) # modify class attribute print (Test.num) # output result # 100class200 Thank you for reading! On "what is the use of class attributes, class methods and static methods in Python?" this article ends here. I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it out for more people to see!

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