Static methods and class methods in Python

Source: Internet
Author: User

Alas ~ Today, I spent an afternoon comparing static methods and class methods in Python. Finally, I had to spend a long time in the official document. Finally, I realized the differences between the two functions. Alas ~ Why is it so tangled ~ It really hurts to learn a language in depth. Fortunately, I am not planning to study python in depth for the moment.

In a class method, if you use self (in fact, you can use another name, such as this, using self is just a convention, the purpose is to unifyCodeStyle, enhanced readability) as the first parameter, this function will be considered as an instance method in this class and cannot be directly accessed through the class. If we set the first parameter to Cls (same as self, it is also a Convention), this function will be considered as a class function in the class. At the same time, this function requires the classmethod function to convert it into a real class method (usually decorator ). If we do not take the class or instance as the first parameter, this function will be treated as a static function. At the same time, it is similar to the class method, we need to use the staticmethod function to convert this function into a real static method.

Therefore, we can see that when defining static methods, we do not use classes or instances as the first parameter to pass a method like class methods or instance methods, the difference is here. Because the first parameter in the class method is a class, we can use this reference in the function. That is to say, when we need to use class references, we need to use class methods. You can use static methods when you do not need to use class references. As described in static methods and class methods in the descriptor howto guide in the official document:

Static Method: "There may be useful functions which are conceptually related but do not depend on the data ."

Method: "This behavior is useful whenever the function only needs to have a class reference and does not care about any underlying data ."

Of course, this is not absolute. We can also use static methods to complete the class method. After all, we can define a variable in the static method as a class reference. Therefore, I think the class method is just a static method that uses class references. It is similar to the syntax sugar. Some people may say that static methods are not bound to classes, but class methods are bound everywhere. However, in my opinion, both static methods and class methods survive in a unified namespace, that is, they all have a binding relationship with the class. However, because the first parameter of the class method is a class, the class method has another binding relationship to the class.

Python beginners, personal opinion, do not like, Do not spray, please correct the error, study and discuss together.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.