In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
Python how to achieve class static methods and class methods, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can gain something.
I. the jobs monitored by the properties of the class
Case: make a property value of a class greater than 10
Train of thought:
1. Set up a class
two。 Define a _ _ setattr__ method to monitor properties
3. Instantiate the class and set the properties of the class
4. Check the value of the property of the class to see whether the property monitoring is successful or not
2. Static methods of classes
Precede the method name in the class with a header tag @ staticmethod. Static methods are often used in some custom classes to achieve some general functions, which makes it convenient for us to call the methods of the class. Static methods of a class do not need parameters such as self, because static methods of a class can be called without instantiation.
Third, the class method of class
Precede the method name in the class with a header tag @ classmethod.
The invocation of the class method of the class does not need to be instantiated.
The class method of a class is a supplement to the constructor of the class in python.
Definition and invocation of static methods
Definition:
@ staticmethod
The method name of the def class:
Method subject
Give an example
@ staticmethod
Def say ():
Print ("We are static methods, and I don't need to instantiate to be called")
Call:
The name of the class. Method name ()
Person.say ()
Class methods are similar to static methods
Definition:
@ classmethod
Def setName (cls):
Name= "test class methods"
Print (name)
Call:
Person.setName ()
The main difference between a static method of a class and a class method is that the header tag is different when the method is defined. But when defining a class method, you need a parameter of the class itself.
Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.