In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly explains the "Python class methods and class method static method instance analysis", the content of the article is simple and clear, easy to learn and understand, now please follow the editor's train of thought slowly in depth, together to study and learn "Python class methods and class method static method instance analysis"!
I. Preface
Class methods can also be divided into class methods, instance methods, and static methods.
Second, detailed explanation of the case.
Example method
In general, methods defined in a class are instance methods by default.
Example:
Class CLanguage: # class constructor, which also belongs to the instance method def _ _ init__ (self): self.name = "Baidu" self.add = "www.baidu.com" # below defines a say instance method def say (self): print ("calling say () instance method")
Call the say () method to implement the function.
Clang = CLanguage () clang.say ()
Running result:
Note:
The instance method also contains at least one self parameter that binds the instance object that calls the method.
Class method
A Python class method is similar to an instance method in that it contains at least one parameter, except that usually naming it cls,Python in a class method automatically binds the class itself to the cls parameter (note that it is not a class object). So there is no need to explicitly pass parameters for the cls parameter when calling the class method.
The biggest difference from instance methods is that class methods need to be modified with the @ classmethod modifier.
Example:
Class ass: # class constructor, also belongs to instance method def _ _ init__ (self): self.name = "Baidu" self.add = "www.baidu.com" # defines a class method @ classmethod def info (cls): print ("calling class method", cls)
If there is no @ classmethod, the Python interpreter recognizes the fly () method as an instance method rather than a class method.
Class methods are recommended to be called directly with the class name.
# call the class method ass.info () directly with the class name # call the class method clang = ass () clang.info () using the class object
Running result:
Class static method
Static methods are defined in the class space (class namespace), while functions are defined in the space where the program is located (global namespace).
Static methods do not have special parameters such as self or cls, so the Python interpreter does not bind any classes or objects to the parameters it contains.
Static methods need to be modified with @ staticmethod.
Example:
Class ass: @ staticmethod def info (name, add): print (name, add)
Static methods can be called in both class name and class object.
# call static method ass.info ("Baidu 1", "www.baidu.com") directly with class name # call static method clang = ass () clang.info ("Baidu 2", "www.baidu.com") using class object
Running result:
Thank you for your reading, the above is the content of "Python class methods and class method static method instance analysis". After the study of this article, I believe you have a deeper understanding of the problem of Python class methods and class method static method instance analysis, and the specific use needs to be verified by practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.