Python instance methods, class methods, and Static methods

Source: Internet
Author: User

When learning Python code, the first parameter in the method of seeing some classes is the CLS, some self, knowing that Python does not restrict the first parameter name of a method in a class, either self or CLS, but according to people's customary usage, Self is generally used in instance methods, while the CLS is generally used in class methods, and in static methods it is not necessary to use a default parameter. In the following code, in the method of the Instancemethod class, the first parameter is the default self, which can be represented by any name, without any effect. When the class is called, it is necessary to satisfy the number of parameters (the parameter contains *args exceptions), such as 13 lines, when the class calls no parameters, it will prompt an error. Similarly, the number of arguments to an instance method should also satisfy the requirements, such as an error in 16 rows. One of the main characteristics of an instance method is that it needs to be bound to an object, and the Python parser automatically passes the instance itself to the method, as shown in line 14, and calling the method directly using the INSTANCEMETHOD.F1 () is not possible.

1 #Coding:utf82 classInstancemethod (object):3 4     def __init__(Self, a):5SELF.A =a6 7     defF1 (self):8         Print 'This is {0}.'. Format (self)9 Ten     defF2 (Self, a): One         Print 'value:{0}'. Format (a) A  - if __name__=='__main__': -     #im = Instancemethod () theim = Instancemethod ('233') - im.f1 () -     #im.f2 () -IM.F2 (233)
View Code

Both static and class methods need to use decorators, respectively, Staticmethod and Classmethod. The static method is not related to the class, I think it is the general method of wrapping in the class, in the following example, it is possible to call the static method to use the instance and not use the instance. Class method, the default first parameter uses the CLS, and the class method can be called directly using the class without requiring an instance. For these three different methods, use the method as shown in the following example. So the question is, since there is an instance method, what are the advantages of class and static methods compared to this?

In a class method, either using an instance or a class invocation method, the class is passed in as the first parameter, which is the class itself. If you inherit the class that uses the class method, all subclasses of the class will have this method, and this method will automatically point to the subclass itself, which is very useful in the factory function. Static methods, which are not related to the class and the instance, can be replaced with a generic method, but using static methods can better organize the code to prevent the code from becoming larger and confusing. Class methods can be substituted for static methods. Static methods cannot be modified in inheritance.

1 classTest (object):2 3     defInstance_method (self):4         Print 'This is {0}'. Format (self)5 6 @staticmethod7     defStatic_method ():8         Print 'This is static method.'9 Ten @classmethod One     defClass_method (CLS): A         Print 'This is {0}'. Format (CLS) -  -  the if __name__=='__main__': -A =Test () - A.instance_method () - A.static_method () + A.class_method () -     Print '----------------------------------------' +     #Test.instance_method () A Test.static_method () atTest.class_method ()
View Code

Python instance methods, class methods, and Static methods

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.