What is the difference between @staticmethod and @classmethod in Python?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
@staticmethod and @classmethod both define methods on a class, but they receive different context and solve different design problems. staticmethod behaves like a namespaced utility function, while classmethod receives the class object and is often used for alternate constructors or class-level behavior. Choosing correctly keeps APIs extensible and inheritance-friendly.
Core Binding Difference
A normal instance method receives self. The two decorators change that binding:
@staticmethodreceives no automatic first argument.@classmethodreceivesclsas first argument.
This binding difference drives most design decisions.
When to Use @staticmethod
Use static methods when logic belongs conceptually to class domain but does not need class or instance state.
This could be a module-level function too. staticmethod is mainly an organizational choice.
Good static method traits:
- pure utility logic
- no dependency on mutable class variables
- no need for subclass-specific behavior
When to Use @classmethod
Use class methods when behavior should vary with subclass or when constructing class instances from alternative inputs.
Because method calls cls(...), subclasses automatically get correct instance type when they inherit this constructor.
Inheritance Behavior Difference
Inheritance highlights why class methods are powerful.
Dog.create produces Dog instance because cls refers to subclass at call time.
A static method cannot do this automatically unless subclass type is passed manually.
Practical API Design Patterns
Common classmethod patterns:
- alternate constructors from JSON, CSV, environment, or DB row
- class-level caches or registries
- factory behavior requiring subclass-aware dispatch
Common staticmethod patterns:
- validation helpers
- deterministic text formatting
- small conversion utilities near model code
If method reads or mutates class state, classmethod is usually the right choice.
Interaction with Dataclasses and Validation
In dataclass-heavy code, class methods are often used for richer construction flows.
This separation keeps construction and validation concerns clear.
Testing and Mocking Considerations
Both method types are easy to test, but semantics differ:
- static methods are usually pure and isolated
- class methods may depend on class attributes or subclass overrides
When refactoring, class methods preserve polymorphism better than static methods in inheritance-heavy designs.
Common Pitfalls
A common pitfall is using staticmethod for alternate constructors, which blocks subclass-friendly instantiation. Another is using classmethod for utility logic that never touches class state, adding unnecessary complexity. Teams also sometimes convert module-level functions into static methods only for style consistency, reducing reuse across modules. Finally, unclear method intent can confuse API users who expect instance behavior from class-level methods.
Summary
@staticmethodhas no automaticselforclsbinding.@classmethodreceivesclsand supports subclass-aware behavior.- Use static methods for utility logic independent of class state.
- Use class methods for alternate constructors and class-level workflows.
- Choose based on semantic intent, not decorator popularity.
Related reading
- What is the difference between __str__ and __repr__?
- >, <, >= and <= don''t work with filter in Django
- __init__ got an unexpected keyword argument 'cachedir' when importing top2vec
- __str__ versus __unicode__
- _csv.Error field larger than field limit 131072
- _pickle.PicklingError Could not serialize object TypeError can't pickle _thread.RLock objects
- _tkinter.TclError no display name and no DISPLAY environment variable
- / vs // for division in Python
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.