site logo

Ask. Code. Learn. Grow with the Developer Community.


Category: (All)
❮  Go Back

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



1






Showing comments related to this post.

_coldshadow44

2025-10-15

1. Regular Methods (self)

A normal method in Python automatically receives the instance as its first argument (self).

This means it can access instance attributes and modify them.

class A:
def foo(self, x):
print(f"executing foo({self}, {x})")

a = A()
a.foo(1)
# Output: executing foo(<__main__.A object at 0x...>, 1)

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.

class A:
@classmethod
def class_foo(cls, x):
print(f"executing class_foo({cls}, {x})")

a = A()
a.class_foo(1)
# Output: executing class_foo(<class '__main__.A'>, 1)

A.class_foo(1)
# Output: executing class_foo(<class '__main__.A'>, 1)

This means you can call a class method either from an instance or directly from the class.

They are typically used for:

  1. Alternative constructors
  2. Operations that affect the entire class rather than one instance

Example of an alternative constructor:

class Person:
def __init__(self, name, age):
self.name = name
self.age = age

@classmethod
def from_birth_year(cls, name, year):
from datetime import date
return cls(name, date.today().year - year)

p = Person.from_birth_year("Alice", 1995)
print(p.age)

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.

class A:
@staticmethod
def static_foo(x):
print(f"executing static_foo({x})")

a = A()
a.static_foo(1)
# Output: executing static_foo(1)

A.static_foo('hi')
# Output: executing static_foo(hi)

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:

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

print(MathUtils.add(5, 3)) # Output: 8

4. Bound vs Unbound Methods

Here’s what happens internally when you inspect these methods:

a = A()
print(a.foo) # <bound method A.foo of <__main__.A object>>
print(a.class_foo) # <bound method type.class_foo of <class '__main__.A'>>
print(a.static_foo) # <function static_foo at 0x...>
  1. a.foo is bound to the instance a.
  2. a.class_foo is bound to the class A.
  3. a.static_foo is not bound — it’s just a plain function.





Member's Sites: