Class method vs Static method in Python

Class method

A class method receives the class as implicit first argument, just like an instance method receives the instance. This is useful when you want the method to be a factory for the class: since it gets the actual class it was called on as first argument, you can always instantiate the right class, even when subclasses are involved.

To declare a class method:

class MyClass: @classmethod def f(cls, arg1, arg2, ...): ...

The @classmethod form is a function decorator.

Static method

A staticmethod is a method that knows nothing about the class or instance it was called on. It just gets the arguments that were passed, no implicit first argument. It is very similar to a module level function. It is callable without instantiating the class first. It’s definition is immutable via inheritance.
  1. Python does not have to instantiate a bound-method for object.
  2. It eases the readability of the code, and it does not depend on the state of object itself.

To declare a static method:

class MyClass: @staticmethod def f(arg1, arg2, ...): ...

The @staticmethod form is a function decorator.

Class Method Vs. Static Method

Difference between @staticmethod and @classmethod | Python
  1. The class method takes cls (class) as first argument while the static method does not take any specific parameter.
  2. Class method can access and modify the class state whereas Static Method cannot access or modify the class state.
  3. The class method takes the class as parameter to know about the state of that class, but static methods do not know about class state. These methods are used to do some utility tasks by taking some parameters.
  4. @classmethod decorator is used in Class Method and @staticmethod decorator is used in Static Method.