Get the App
SLTechnology News&Howtos  ›  Development  › 

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

Shulou Source: shulou.com Published: 2022-06-02 19:43:44 10月05日 Update

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!

Tags: Properties methods static objects eyes content more articles output good practical personal fields quantity articles for example look knowledge results blue Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux macOS OPPO Reno MySQL Microsoft