SoFunction
Updated on 2024-11-16

Detailed explanation of @classmethod and @staticmethod methods in python

In the python class, often encountered @classmethod and @staticmethod these two decorators, so in the end the difference between them and what is the role of it? Specifically look at the following.

  • @classmethod : defaults to a cls parameter, which can be called with either a class or an object.
  • @staticmethod: static method, no default parameters, callable with both class and object.

1.@staticmethod:

Let's look at the code:

class A:
    def f1(x):
        print(x)
         
A.f1(2)  # 2  resemble.function (math.)

Create a class and call functions through the class.

class A:
    @staticmethod
    def f1(x):
        print(x)
         
A.f1(2)  # 2 Classes. Static Methods
A().f1(2)  # 2 boyfriend.static method  This situation is enforceable,If the abovef1not having beenstaticmethodDecorate then an error will be reported!!!

Create a class and call the function through the class. Also, because the method is decorated with the staticmethod decorator, then the method called through object . Method is also callable.

So in a class, a function decorated by @staticmethod can be called directly by the class and also by the instantiated object!!!

Also, it was found that @staticmethod decorated functions don't need to pass the self argument at all. This is because functions decorated by @staticmethod are bound directly to the class and not to the object.

2.@classmethod:

class A:
    @classmethod
    def f1(cls,x):
        print(x)
         
A.f1(2)  # 2 Classes. Methods
A().f1(2) # 2  boyfriend.methodologies

Create a class and call the function through the class. Also, because the method is decorated by the classmethod decorator, then the method called through object . Method is also callable. But note that in the decorated method, the cls parameter must be passed!!!!

class B:
    name = 'bruce'
    age = 16
    @classmethod
    def f1(cls,x):
        print(x)
        print()
        print()
B().f1(1)
# 1
# 16
# bruce

In the above, it is illustrated that the method decorated by classmethod, by means of the cls parameter, in which the properties of the class can be called.

class C:
 
    @classmethod
    def f1(cls,x):
        print(x)
        cls().f2()
 
    def f2(self):
        print('hello world')
 
C.f1(1) or C().f1(1)# 1<br># hello world

In the above, it is illustrated that the method decorated by classmethod, through the cls parameter, in which other methods of the class can be called.

So in a class, a function decorated by @classmethod must first be passed the first parameter cls in the method, which can be called directly by the class or by the object !!!!

Also, because a cls is passed, other properties and methods in the class can be called.

to this article on the detailed python @classmethod and @staticmethod method of the article is introduced to this, more related python @classmethod and @staticmethod content, please search for my previous articles or continue to browse the following related articles I hope you will support me more in the future!