python
programming
staticmethod
classmethod
python-decorators

What is the difference between staticmethod and classmethod in Python?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Both @staticmethod and @classmethod define methods that you can call on the class itself, but they are not the same thing. A staticmethod gets no automatic first argument, while a classmethod receives the class as its first argument, usually named cls.

That difference determines when each one is appropriate. If the method needs class-aware behavior, use classmethod. If it is just a utility function that happens to live in the class namespace, use staticmethod.

@staticmethod: No self, No cls

A static method behaves like a plain function stored on the class.

python
1class MathTools:
2    @staticmethod
3    def add(a, b):
4        return a + b
5
6print(MathTools.add(2, 3))

The method does not receive an instance and does not receive the class automatically. It can still access global names or imported modules, but it has no built-in awareness of class state.

This makes staticmethod a good fit for helper logic that is conceptually related to the class but does not depend on object or class identity.

@classmethod: Gets the Class as cls

A class method receives the class itself.

python
1class Person:
2    species = "human"
3
4    @classmethod
5    def describe_species(cls):
6        return cls.species
7
8print(Person.describe_species())

Because it gets cls, a class method can:

  • read class attributes
  • modify class-level state
  • create instances using the actual subclass that called it

That last use is why classmethod is common for factory methods.

Factory Method Example

python
1class Person:
2    def __init__(self, name):
3        self.name = name
4
5    @classmethod
6    def from_uppercase(cls, raw_name):
7        return cls(raw_name.title())
8
9p = Person.from_uppercase("ALICE")
10print(p.name)

Using cls(...) instead of hard-coding Person(...) makes the method subclass-friendly.

Why classmethod Helps with Inheritance

python
1class Animal:
2    def __init__(self, name):
3        self.name = name
4
5    @classmethod
6    def create(cls, name):
7        return cls(name)
8
9class Dog(Animal):
10    pass
11
12pet = Dog.create("Rex")
13print(type(pet).__name__)

This prints Dog, not Animal, because cls refers to the class that invoked the method.

A staticmethod does not have that behavior automatically.

How They Behave When Called

Both decorators let you call the method on the class, and both can also be accessed through an instance. The important difference is still what gets passed automatically.

python
1class Demo:
2    @staticmethod
3    def s():
4        return "static"
5
6    @classmethod
7    def c(cls):
8        return cls.__name__
9
10d = Demo()
11print(Demo.s())
12print(Demo.c())
13print(d.s())
14print(d.c())

Calling through an instance does not suddenly make a static method receive self. It stays argument-free unless you pass something explicitly. A class method still receives the class, not the instance.

This is useful to remember because both methods are descriptors, but they bind differently.

When to Choose Which

Use @staticmethod when:

  • the logic does not need self
  • the logic does not need cls
  • the method is just namespaced utility behavior

Use @classmethod when:

  • the logic needs class attributes
  • the method should respect subclasses
  • you are building alternative constructors or factories

If the method needs instance state, it should usually be a normal instance method instead.

Common Pitfalls

The biggest mistake is using staticmethod for factory-style methods that really should be class-aware. That makes subclassing less flexible.

Another common issue is using classmethod even when the method does not need cls at all. That adds unnecessary coupling to the class.

People also sometimes think staticmethod is about performance. In practice, the design question is semantic: what information should the method receive automatically?

Finally, if a method needs instance attributes such as self.name, neither decorator is the right choice. Use a regular instance method.

Summary

  • 'staticmethod gets no automatic first argument.'
  • 'classmethod receives the class as cls.'
  • Use staticmethod for utility behavior that does not depend on class or instance state.
  • Use classmethod for class-aware logic and factory methods.
  • 'classmethod works better with inheritance because it uses the calling class.'
  • If the method needs object state, use a normal instance method instead.

Course illustration
Course illustration

All Rights Reserved.