๐ Python Class Methods vs Static Methods: What, When, and Why
When working with Pythonโs object-oriented features, youโll often encounter two decorators that seem deceptively similar: @classmethod andโฆ
๐ Python Class Methods vs Static Methods: What, When, and Why
When working with Pythonโs object-oriented features, youโll often encounter two decorators that seem deceptively similar: @classmethod and @staticmethod. While both are methods that donโt operate on the instance level like typical methods, they serve distinct purposes.
In this post, weโll break down:
- What class methods and static methods are
- When to use each of them
- Why do they matter for clean, maintainable code
Weโll also cover the key differences with simple, practical examples โ no fluff, just Pythonic clarity.
Non-members? Read this story free with this link ๐ Non-members link

๐ง First, Know the Three Types of Methods
In Python, you can define three types of methods inside a class:
1. Instance Methods (most common)
- Defined without any decorator.
- The first parameter is
self, which refers to the specific instance of the class. - Can access and modify instance attributes and class attributes.
2. Class Methods
- Defined using the
@classmethoddecorator. - The first parameter is
cls, which refers to the class itself, not an instance. - Used to access or modify class-level data, and often used as factory methods.
3. Static Methods
- Defined using the
@staticmethoddecorator. - No
selforclsparameter. - Acts like a regular function, just logically grouped inside a class.
๐งช Class Method in Action
class Book:
total_books = 0
def __init__(self, title):
self.title = title
Book.total_books += 1
@classmethod
def get_total_books(cls):
return cls.total_books
โ What It Does:
- Accesses
cls.total_books, which are shared across all instances. - It could also be used to instantiate the class differently (see factory method example below).
๐ When to Use Class Methods:
- When you need to operate on class variables.
- When writing a factory method that returns a new instance with some pre-defined logic.
Example of a Factory Class Method:
class User:
def __init__(self, name, age):
self.name = name
self.age = age
@classmethod
def from_birth_year(cls, name, birth_year):
from datetime import date
age = date.today().year - birth_year
return cls(name, age)
# Usage
user = User.from_birth_year("Alice", 1995)
โ๏ธ Static Method in Action
class MathUtils:
@staticmethod
def add(x, y):
return x + y
โ What It Does:
- Doesnโt access class (
cls) or instance (self) at all. - Behaves just like a regular function, but itโs inside a class because it logically belongs there.
๐ When to Use Static Methods:
- When the method doesnโt need access to instance or class data, but it belongs conceptually to the class.
- To keep related utility functions scoped within your class.
Another Real-World Example:
class Temperature:
@staticmethod
def celsius_to_fahrenheit(c):
return (c * 9/5) + 32
Even though this method has nothing to do with the internal state of the class, keeping it in Temperature improves cohesion and discoverability.
๐ Key Differences Recap (No Table Needed)
Hereโs a concise way to remember how they differ:
1. Instance Method (self)
- Works with individual object state.
- Can access and modify class and instance attributes.
2. Class Method (cls)
- Works with the class as a whole.
- Can access/modify class-level data.
- Useful for factory patterns or alternate constructors.
3. Static Method
- Doesnโt touch
selforcls. - Like a regular function, but lives inside the class for logical grouping.
๐ก Design Insights
- If your method uses
self, it's obviously an instance method. - If your method only touches class-level variables or is a factory, make it a class method.
- If your method doesnโt touch either but is still related to the classโs domain, itโs a good candidate for a static method.
- Overusing static methods often signals that functionality might be better served outside the class, or that you need a rethink of your design.
๐ Bonus Tip: Use Class Methods to Improve Testability
Static methods are harder to mock in tests because they donโt support inheritance or polymorphism as well as class methods. Prefer class methods when you might want to override or mock behaviour in subclasses or tests.
โ In Summary
Python gives you the flexibility to choose between instance, class, and static methods โ each with its purpose. Understanding the differences is not just academic โ it directly affects the readability, extensibility, and testability of your code.
Next time youโre writing a utility or factory method, pause and ask:
- Does this method need access to the class or instance?
- Could this live outside the class, or is it conceptually tied to it?
Write with intent, and your code will thank you later.
๋ฉํ๋ฐ์ดํฐ
- post_id
- ebfd72641e57
- slug
- python-class-methods-vs-static-methods-what-when-and-why-ebfd72641e57
- url
- https://python.plainenglish.io/python-class-methods-vs-static-methods-what-when-and-why-ebfd72641e57
- canonical_url
- https://python.plainenglish.io/python-class-methods-vs-static-methods-what-when-and-why-ebfd72641e57
- author_url
- https://medium.com/@rohanmistry231
- status
- ok
- fetched_at
- 2026-08-17 05:19:22