Difference Between @staticmethod and @classmethod in Python Explained with Examples
In Python’s object-oriented programming (OOP), decorators like @staticmethod and @classmethod are often confusing for beginners.
Both allow you to define methods that can be called without creating an instance of a class, but they differ in what they receive automatically when called — the class or the instance.
Understanding the difference between these two is essential for writing cleaner, more maintainable Python code, especially when designing reusable classes and alternative constructors.
Difference Between staticmethod and classmethod in Python Explained with Examples
coldshadow44 on 2025-10-15
Make a comment
_
2025-10-15
A normal method in Python automatically receives the instance as its first argument (self).
This means it can access instance attributes and modify them.
Here, a (the instance) is implicitly passed to foo as self.
2. Class Methods (cls)
A class method, marked with the @classmethod decorator, automatically receives the class itself (cls) as the first argument — not the instance.
This means you can call a class method either from an instance or directly from the class.
They are typically used for:
Example of an alternative constructor:
3. Static Methods
A static method does not receive self (instance) or cls (class).
It behaves like a regular function that happens to live inside a class’s namespace.
You use static methods to group utility functions that are logically related to a class, even though they don’t modify or depend on the class or instance.
Example:
4. Bound vs Unbound Methods
Here’s what happens internally when you inspect these methods: