โ† Back to list

๐Ÿ” 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โ€ฆ

Rohan Mistry in Python in Plain English ยท 2025-04-25 08:06 ยท 0 claps ยท 2.9 min read paywalled
#python #object-oriented #class-method #static-methods #clean-code
Open on Medium โ†—

๐Ÿ” 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 isself, which refers to the specific instance of the class.
  • Can access and modify instance attributes and class attributes.

2. Class Methods

  • Defined using the @classmethod decorator.
  • The first parameter iscls, 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 @staticmethod decorator.
  • No self or cls parameter.
  • 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:

  • Accessescls.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 self or cls.
  • Like a regular function, but lives inside the class for logical grouping.

๐Ÿ’ก Design Insights

  • If your method usesself, 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