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 to use class methods and static methods in python

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

Share

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

Editor to share with you how to use class methods and static methods in python. I hope you will get something after reading this article. Let's discuss it together.

Class method

Class People: country='China' # Class methods are modified with classmethod @ classmethod # denotes the following methods as class methods def get_country (cls) through identifiers: # habitually use cls return cls.country # to access class properties pass passprint (People.get_country ()) # to reference p=People () print through class objects ('instance object access% slots% p. Get_country () # access through instance object

Class People: country='China' # Class methods are decorated with classmethod @ classmethod # the following methods are denoted by identifiers as class methods def get_country (cls): # habitually use cls return cls.country # to access the class attribute pass @ classmethod def change_country (cls Data): cls.country=data # modify the value of the class attribute in the class method passprint (People.get_country ()) # reference p=People () print ('instance object access% s'%p.get_country ()) People.change_country (' English') print (People.get_country ()) through the class object

Static method

Class People: country='China' # Class methods are decorated with classmethod @ classmethod # the following methods are denoted by identifiers as class methods def get_country (cls): # habitually use cls return cls.country # to access the class attribute pass @ classmethod def change_country (cls Data): cls.country=data # modify the value of a class attribute in the class method pass @ staticmethod def getData (): # No need to pass the parameter return People.country passprint (People.getData ()) # you can access # print (People.get_country ()) # refer to p=People () print (People.getData ()) through the class object. Will access static methods through instance objects

Why use static methods?

Because static methods are mainly used to store logical code itself, there is no interaction with classes and instance objects

That is to say, the operations of methods and properties in the class are not involved in static methods.

Data resources can be effectively and fully utilized.

# demo returns the current system time import time # introducing the time module class TimeTest: def _ _ init__ (self,hour,min,second): self.hour=hour self.min=min self.second=second @ staticmethod # is directly defined as a static method that does not require instance attribute def showtime (): return time.strftime ('% HRV% MRV% S' Time.localtime () passprint (TimeTest.showtime ()) t=TimeTest (2Jing 10pm 15) print (t.showtime ()) # there is no need to directly use static method output or import time

Review

After reading this article, I believe you have a certain understanding of "how to use class methods and static methods in python". If you want to know more about it, you are welcome to follow the industry information channel. Thank you for reading!

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