Static Methods vs Class Methods in Python
Compare Python static methods and class methods: what cls means, factory constructors, inheritance behavior and when to use each, with code examples.
Expected Interview Answer
A class method (@classmethod) receives the class as its first argument (cls) and can access or modify class state, while a static method (@staticmethod) receives neither instance nor class and behaves like a plain function grouped inside the class.
Both are defined with decorators and can be called on the class or an instance. A class method's cls makes it ideal for factory methods and for working with class-level attributes, and it respects inheritance because cls is the actual subclass used. A static method takes no self or cls; it is simply a utility that logically belongs to the class's namespace but does not touch instance or class data. A regular (instance) method, by contrast, takes self and works with per-object state.
- Class methods enable alternative constructors (factory methods)
- Class methods access and mutate shared class state via cls
- Class methods respect inheritance because cls is the calling subclass
- Static methods group related utility logic inside a class
- Static methods avoid unnecessary self/cls when no state is needed
AI Mentor Explanation
A class method is like the team selector who works with the whole squad's policy — picking a fresh eleven from the roster, a factory for new line-ups. A static method is the fixed lbw rule printed in the rulebook: it belongs to the sport and applies the same way regardless of which team or player invokes it, needing no squad or individual context.
Step-by-Step Explanation
Step 1
Define an instance method
A normal method takes self and works with per-object attributes; it is the default kind.
Step 2
Add @classmethod
Decorate a method and make its first parameter cls; Python passes the class automatically.
Step 3
Use cls for factories
Inside a class method, call cls(...) to build and return new instances — an alternative constructor.
Step 4
Add @staticmethod
Decorate a method that takes neither self nor cls; it is a plain function living in the class namespace.
Step 5
Call either way
Both can be invoked on the class (Klass.method()) or on an instance (obj.method()); cls or nothing is bound accordingly.
What Interviewer Expects
- That classmethod receives cls and staticmethod receives neither self nor cls
- Class methods used as factory / alternative constructors
- How cls respects inheritance in subclasses
- When a static method is appropriate (no instance or class state needed)
- Difference from a regular instance method that takes self
Common Mistakes
- Thinking a static method receives self or cls
- Using a static method when the logic needs class state (should be a class method)
- Forgetting the @classmethod or @staticmethod decorator
- Naming the class-method parameter self instead of cls
- Assuming a class method cannot be overridden or affected by inheritance
Best Answer (HR Friendly)
“In Python, a class method works with the class itself and is often used to create objects in a special way, while a static method is just a helper function that lives inside the class but doesn't touch the object or the class. You pick a class method when you need the class, and a static method when you only need the inputs.”
Code Example
class Pizza:
def __init__(self, toppings):
self.toppings = toppings
# class method: an alternative constructor (factory)
@classmethod
def margherita(cls):
return cls(['tomato', 'mozzarella']) # cls is Pizza (or a subclass)
# static method: pure utility, no self or cls
@staticmethod
def area(radius):
return 3.14159 * radius * radius
# regular instance method uses self
def describe(self):
return f"Pizza with {', '.join(self.toppings)}"
p = Pizza.margherita() # factory call on the class
print(p.describe()) # Pizza with tomato, mozzarella
print(Pizza.area(10)) # 314.159 -- no instance needed
class DeepDish(Pizza):
pass
print(type(DeepDish.margherita())) # <class '__main__.DeepDish'> -- cls respects inheritanceFollow-up Questions
- When would you use a class method instead of a static method?
- How does a class method enable an alternative constructor?
- Why does cls make class methods inheritance-aware?
- Can a static method access instance attributes? Why or why not?
- How does an instance method differ from both in its first parameter?
MCQ Practice
1. What does the first parameter of a @classmethod refer to?
A class method automatically receives the class as its first argument, conventionally named cls.
2. Which is the best use case for a @staticmethod?
A static method takes neither self nor cls, so it suits pure utility logic grouped inside the class namespace.
3. Why are class methods useful as factory methods?
Because cls is the calling class, cls(...) constructs the correct type even for subclasses, making inheritance-aware factories.
Flash Cards
What does @classmethod receive? — The class as its first argument (cls); it can read and modify class state and build instances.
What does @staticmethod receive? — Neither self nor cls — it is a plain function grouped inside the class namespace.
Common class-method use? — Factory / alternative constructor: cls(...) builds and returns a new instance, respecting inheritance.
When to prefer a static method? — When the logic depends only on its arguments and needs no instance or class state.