A brief introduction to the instance method class method and static method in python

  • 2020-05-26 09:26:46
  • OfStack

In learning python code, see some kind of method of parameter is 1 cls, have a plenty of self, after understanding that python is not the first parameter to the method of class name restrictions, can be a self, also can be cls, but according to the idiomatic usage of people, like self1 is used in instance methods, while cls 1 used in the method, in a static method does not need to use a default parameters. In the code below, the first parameter in the method of the InstanceMethod class is the default self, where self can be represented by any name without any effect. When a class is called, the number of parameters must be met (*args is the exception). For example, in line 13, when the class call has no parameters, it will prompt an error. Similarly, the number of arguments to the instance method should satisfy the requirement, for example, an error will be reported in line 16. One of the main features of the instance method is that it needs to be bound to an object. The python parser automatically passes the instance itself to the method, as shown in line 14, instead of calling the method directly using InstanceMethod.f1 ().


 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 use modifiers, staticmethod and classmethod, respectively. Static methods have nothing to do with the class, I think it is wrapped in a class 1 method. In the following example, it is ok to call a static method using an instance and not using an instance. In class methods, the default first parameter is cls, and class methods can be called without an instance. For these three different methods, use the method shown in the following example. So the question is, now that you have instance methods, what are the advantages of class methods versus static methods?

In a class method, either using an instance or calling a method with a class, the class is passed in as the first argument, which is the class itself. If you inherit from a class that USES a class method, all subclasses of that class will have the method, and the method will automatically point to the subclass itself, which is useful in factory functions. Static methods have nothing to do with classes and instances, and can be completely replaced by 1 general method. However, using static methods can better organize the code and prevent it from getting messy when the code gets bigger. Class methods are an alternative to 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()

Related articles: