Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

Case Analysis of Python Class methods and static methods

Shulou Source: shulou.com Published: 2022-05-31 10:43:12 10月01日 Update

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!

Tags: Method instance static parameter object instance analysis analysis space class name result learning running content situation being interpreter interpretation different maximum special Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei macOS Linux Shulou Information NVidia