The roles and differences between staticmethod and classmethod in Python

  • 2020-12-10 00:46:15
  • OfStack

Generally speaking, to use a method of a class, you need to instantiate an object before calling the method.

With @staticmethod or @classmethod, you can use the class name without instantiation. Method name () to call.

This is good for organizing code, putting functions that belong to a class into that class, and for keeping namespaces clean.

Since @staticmethod and @classmethod can both be direct class names. Method name (), so what's the difference

From the point of view of their use

@staticmethod does not need to represent the self argument of its own object and the cls argument of its own class, just as it does with function 1. The @classmethod parameter also does not need the self parameter, but the first parameter needs to be the cls parameter representing its own class.

If you want to call one of the property methods of this class in @staticmethod, you can only call the class name directly. Property or class name. Method name.

Since @classmethod holds the cls parameter, it can call properties of the class, methods of the class, instantiate objects, and so on, avoiding hard coding.

To understand what instance, static, and class methods are:


class Demo(object):
 def instance_method(self, your_para):
 """
 this is an instance_method
 you should call it like the follow:
 a = Demo()
 a.instance_method(your_para)
 plus: in python, we denote 'cls' as latent para of Class
 while 'self' as latent para of the instance of the Class
 :param your_para: 
 :return: 
 """
 print("call instance_method and get:", your_para)
 @classmethod
 def class_method(cls, your_para):
 """
 this is a class_method
 you can call it like the follow:
 method1:
 a = Demo()
 a.class_method(your_para)
 method2:
 Demo.class_method
 plus: in python, we denote 'cls' as latent para of Class
 while 'self' as latent para of the instance of the Class
 :param your_para: 
 :return: 
 """
 print("call class_method and get:", your_para)
 @staticmethod
 def static_method(your_para):
 """
 this is a static_method and you can call it like the 
 methods of class_method
 :param your_para: 
 :return: 
 """
 print("call static_method and get:", your_para)

Although class methods are not explicitly declared cls when called, the class itself is actually passed in as an implicit argument. This is like the instance method being called without explicitly declaring self, but in fact the instance itself is passed in as an implicit argument.

For static functions, we generally define class - and instance-independent functions as static functions. For example, entry-check functions are best defined as static functions.

The beauty of class methods and their role in inheritance:


class Fruit(object):
 total = 0 #  This is a 1 A class attribute 
 @classmethod
 def print_total(cls):
 print('this is the ', cls, '.total:', cls.total, ' and its id: ', id(cls.total)) # cls It's the class itself, it prints the class properties total The value of the 
 print('this is the Fruit.total:', Fruit.total, 'and its id: ', id(Fruit.total))
 print("=======================")
 @classmethod
 def set(cls, value):
 cls.total = value
class Apple(Fruit):
 pass
class Orange(Fruit):
 pass
app1 = Apple()
app1.set(10)
app1.print_total()
Apple.print_total()
Fruit.set(2)
app1.print_total()
Fruit.print_total()
"""
output:
this is the <class '__main__.Apple'> .total: 10 and its id: 1355201264
this is the Fruit.total: 0 and its id: 1355200944
=======================
this is the <class '__main__.Apple'> .total: 10 and its id: 1355201264
this is the Fruit.total: 0 and its id: 1355200944
=======================
this is the <class '__main__.Apple'> .total: 10 and its id: 1355201264
this is the Fruit.total: 2 and its id: 1355201008
=======================
this is the <class '__main__.Fruit'> .total: 2 and its id: 1355201008
this is the Fruit.total: 2 and its id: 1355201008
=======================
"""

conclusion


Related articles: