Explain the difference between @staticmethod and @classmethod?

Last updated 3 weeks, 1 day ago | 21 views 75     5

Tags:- Python

Python | Difference between @staticmethod and @classmethod

staticmethod is a method that knows nothing about the class or the instance it was called on. It just gets the arguments that were passed, no implicit first argument. Its definition is immutable via inheritance.

class A:

    @staticmethod
    def func(arg1, arg2, ...): 
        ... 

classmethod, on the other hand, is a method that gets passed the class it was called on, or the class of the instance it was called on, as the first argument. Its definition follows Subclass, not Parent class, via inheritance.

class A:

    @classmethod
    def func(cls, arg1, arg2, ...): 
        ...