English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Briefly Discuss Instance Methods, Class Methods, and Static Methods in Python

When learning Python code, you may find that the first parameter in some class methods is cls, while in others it is self. After investigation, it is learned that Python does not impose any restrictions on the name of the first parameter in class methods, which can be self or cls. However, according to common usage, self is generally used in instance methods, while cls is generally used in class methods, and no default parameter is needed in static methods. In the following code, the first parameter of the method in the InstanceMethod class is the default self, which can be replaced with any name without any effect. When calling the class, it is necessary to meet the requirements for the number of parameters (including*args is an exception), for example13In the line, a class call without parameters will prompt an error. Similarly, the number of parameters for instance methods should also meet the requirements, for example16In the line, an error will also occur. One of the main characteristics of instance methods is that they need to be bound to an object, and the Python parser will automatically pass the instance itself to the method, as14As shown in the line, and directly using InstanceMethod.f1() calling the method is not allowed.

 class InstanceMethod(object):
 def __init__(self, a):
  self.a = a
 def f1(self):
  print 'This is {0}.'.format(self)
 def f2(self, a):
  print 'Value:{0}'.format(a)
if __name__ == '__main__':
 # im = InstanceMethod()
 im = InstanceMethod('233')
 im.f1()
 # im.f2()
 im.f2(233)

Both static methods and class methods need to use decorators, with 'staticmethod' and 'classmethod' respectively.Static methods are unrelated to the class. I think they are general methods wrapped in a class. For example, in the following example, it is okay to call static methods with or without an instance. In class methods, the default first parameter uses cls, and class methods can also be called directly using the class without an instance. For these three different methods, the usage is as shown in the following example. So the question is, since there are instance methods, what are the advantages of class methods and static methods compared to them?

In class methods, whether using instance or class to call the method, the class itself is always passed as the first parameter. This parameter is the class itself. If a class inherits a class method, all subclasses of that class will have this method, and this method will automatically point to the subclass itself, which is very useful in factory functions. Static methods are unrelated to the class and instance, and can be replaced by general methods, but using static methods can better organize the code and prevent the code from becoming chaotic as it grows. Class methods can replace static methods. Static methods cannot be modified in inheritance.

class test(object):
 def instance_method(self):
  print 'This is {0}'.format(self)
 @staticmethod
 def static_method():
  print 'This is static method.'
 @classmethod
 def class_method(cls):
  print 'This is {0}'.format(cls)
if __name__ == '__main__':
 a = test()
 a.instance_method()
 a.static_method()
 a.class_method()
 print '----------------------------------------'
 # test.instance_method()
 test.static_method()
 test.class_method()

This is the full content of this article. I hope the content of this article can bring certain help to everyone's learning or work, and I also hope to support the Yelling Tutorial more!

Statement: The content of this article is from the network, and the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, has not been manually edited, and does not assume any relevant legal liability. If you find any content suspected of copyright infringement, please send an email to: notice#oldtoolbag.com (Please replace # with @ when sending an email to report, and provide relevant evidence. Once verified, this site will immediately delete the content suspected of infringement.)

You May Also Like