Skip to the content.
Understanding Class Methods vs. Static Methods in Python | AI Systems Design From Scratch

Connect with Amin Boulouma Official

AI Systems Design From First Principles - An implementation of AI Systems Design From First Principles | Product Hunt

🏠 Documentation Hub 📝 Engineering Blog 💻 GitHub Repository

Class Methods vs. Static Methods in Python

Amin Boulouma, Software Engineer

In Python, understanding the difference between classmethod and staticmethod is crucial for writing clean, object-oriented code. While both are decorators used to define methods that don’t necessarily behave like standard instance methods, they serve very different purposes.

The Standard Instance Method

Before diving into the decorators, remember that a standard method (defined without a decorator) receives the instance (self) as its first argument.

class MyClass:
    def instance_method(self):
        print(f"I belong to instance: {self}")

What is a Class Method?

A class method is a method that is bound to the class, not the instance. It receives the class itself as the first argument, typically named cls.

Key Characteristics

class Date:
    def __init__(self, day, month, year):
        self.day = day
        self.month = month
        self.year = year

    @classmethod
    def from_string(cls, date_str):
        day, month, year = map(int, date_str.split('-'))
        return cls(day, month, year) # Creates an instance using the class

What is a Static Method?

A static method is a method that behaves like a regular function but lives inside a class namespace. It does not receive any implicit first argument (no self, no cls).

Key Characteristics

class MathUtils:
    @staticmethod
    def add(a, b):
        return a + b

Comparison Summary

Feature Instance Method Class Method Static Method
Decorator None @classmethod @staticmethod
First Argument self (instance) cls (class) None
Access Can modify instance/class Can modify class None
Primary Use Instance logic Factory methods Utility functions

Which one should you choose?

  1. **Use classmethod** when you need to access or modify the class state, or when you need an alternative constructor.
  2. **Use staticmethod** when the logic is related to the class but doesn’t require access to the class or instance state (e.g., validation logic, formatting utilities).
  3. **Use instancemethod** by default when you need to interact with the specific object instance.

Connect with Amin Boulouma Official