Instance method, class method, and static method, and instance method static

Source: Internet
Author: User

Instance method, class method, and static method, and instance method static

When learning python code, we can see that the first parameter in some class methods is cls and the first parameter is self. After understanding it, python does not limit the name of the first parameter in the class, it can be self or cls, but according to people's common usage, self is generally used in the instance method, while cls is generally used in the class method, in static methods, a default parameter is not required. In the following code, in the InstanceMethod class method, the first parameter is the default self. Here, you can replace self with any name to indicate it will not be affected. When a class is called, the number of parameters must be met (the parameter contains * args is an exception). For example, if there is no parameter in the class call in row 13, an error is prompted. Similarly, the number of parameters of the instance method must also meet the requirements. For example, an error is returned in 16 rows. One of the main features of the instance method is that it needs to be bound to an object. The python parser will automatically pass the instance itself to the method, as shown in line 14, and directly use InstanceMethod. f1 () cannot call the method.

 1 class InstanceMethod(object): 2  3     def __init__(self, a): 4         self.a = a 5  6     def f1(self): 7         print 'This is {0}.'.format(self) 8  9     def f2(self, a):10         print 'Value:{0}'.format(a)11 12 if __name__ == '__main__':13     # im = InstanceMethod()14     im = InstanceMethod('233')15     im.f1()16     # im.f2()17     im.f2(233)

The modifier must be used for both static and class methods. staticmethod and classmethod are used respectively. Static methods have no relationship with the class. I think they are the general methods encapsulated in the class. In the following example, it is okay to call static methods to use instances or not use instances. In class methods, the first parameter by default uses cls, and class methods can be called directly without an instance. The following example shows how to use these three methods. So the question is, now that we have an instance method, what are the advantages of the class method and static method in comparison?

In a class method, the class is passed as the first parameter, whether it is an instance or a class call method. This parameter 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. This feature is very useful in factory functions. Static methods have nothing to do with classes and instances. They can be replaced by general methods. However, static methods can better organize code to prevent confusion when code grows. Class methods can replace static methods. Static methods cannot be modified in inheritance.

 1 class test(object): 2  3     def instance_method(self): 4         print 'This is {0}'.format(self) 5  6     @staticmethod 7     def static_method(): 8         print 'This is static method.' 9 10     @classmethod11     def class_method(cls):12         print 'This is {0}'.format(cls)13 14 15 if __name__ == '__main__':16     a = test()17     a.instance_method()18     a.static_method()19     a.class_method()20     print '----------------------------------------'21     # test.instance_method()22     test.static_method()23     test.class_method()

 

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.