Question
@staticmethod vs @classmethod in Python Explained
Question
In Python, what is the difference between a method decorated with @staticmethod and one decorated with @classmethod?
For example, how do these two method types differ in:
- how they are defined
- what arguments they receive automatically
- when they should be used
- how they behave when called from a class or an instance
Short Answer
By the end of this page, you will understand how @staticmethod and @classmethod work in Python, what each one receives automatically, when to use each, and how they differ from regular instance methods. You will also see practical examples, common mistakes, and a small project that makes the distinction easy to remember.
Concept
In Python, methods inside a class are not all the same. The main difference between @staticmethod and @classmethod is what Python passes to them automatically.
A quick reminder of the three common method types:
- Instance method: works with a specific object and receives
self - Class method: works with the class itself and receives
cls - Static method: receives nothing automatically
Instance method
A normal method inside a class usually looks like this:
class User:
def greet(self):
return "Hello"
When you call user.greet(), Python automatically passes the instance as the first argument. That first argument is usually named self.
Class method
A class method is defined with @classmethod:
class User:
@classmethod
():
cls()
Mental Model
Think of a class like a company.
- An instance method talks to one employee: it gets
self - A class method talks to the company itself: it gets
cls - A static method is just a tool kept in the company office: it does not get either one automatically
Simple analogy
self= this specific objectcls= this class@staticmethod= a helper function stored inside the class because it belongs there conceptually
For example:
- "What is this user's email?" → instance method
- "Create a user from a CSV row" → class method
- "Check whether an email format looks valid" → static method
A good question to ask is:
Does this method need the object, the class, or neither?
- Needs the object → instance method
- Needs the class → class method
- Needs neither → static method
Syntax and Examples
Core syntax
class Example:
def instance_method(self):
return "instance"
@classmethod
def class_method(cls):
return "class"
@staticmethod
def static_method():
return "static"
Example: all three together
class Temperature:
scale = "Celsius"
def __init__(self, value):
self.value = value
def describe(self):
return f"{self.value} degrees {self.scale}"
@classmethod
def freezing_point(cls):
return cls(0)
@staticmethod
():
(celsius * / ) +
weather = Temperature()
(weather.describe())
(Temperature.freezing_point().describe())
(Temperature.celsius_to_fahrenheit())
Step by Step Execution
Consider this example:
class Book:
category = "General"
def __init__(self, title):
self.title = title
@classmethod
def default_book(cls):
return cls("Untitled")
@staticmethod
def is_valid_title(title):
return len(title.strip()) > 0
Now trace these calls:
book = Book.default_book()
valid = Book.is_valid_title("Python Basics")
Step by step
1. Python reads the class definition
It creates the Book class.
categorybecomes a class attribute__init__becomes an instance methoddefault_bookbecomes a class methodis_valid_titlebecomes a static method
Real World Use Cases
Common uses for @classmethod
Alternative constructors
A class method is often used to create objects from different input formats.
class User:
def __init__(self, name, age):
self.name = name
self.age = age
@classmethod
def from_dict(cls, data):
return cls(data["name"], data["age"])
Useful when reading:
- JSON data from an API
- rows from a CSV file
- configuration files
- database records
Class-level configuration
class AppConfig:
debug = False
@classmethod
def enable_debug(cls):
cls.debug = True
This affects the class itself, not one specific object.
Common uses for @staticmethod
Real Codebase Usage
In real codebases, developers usually choose between these method types based on what the method needs to know.
Common @classmethod patterns
Alternative constructors
class Order:
def __init__(self, order_id, total):
self.order_id = order_id
self.total = total
@classmethod
def from_api(cls, payload):
return cls(payload["id"], payload["total"])
This keeps parsing logic close to the class.
Inheritance-friendly creation
class Animal:
def __init__(self, name):
self.name = name
@classmethod
def unnamed(cls):
return cls("Unknown")
class Dog(Animal):
pet = Dog.unnamed()
((pet).__name__)
Common Mistakes
1. Using @staticmethod when cls is needed
Broken example:
class Person:
def __init__(self, name):
self.name = name
@staticmethod
def create(name):
return cls(name)
Problem:
clsdoes not exist in a static method
Fix:
class Person:
def __init__(self, name):
self.name = name
@classmethod
def create(cls, name):
return cls(name)
2. Using @classmethod for logic that does not need the class
This works, but it is misleading:
Comparisons
Method type comparison
| Method type | First automatic argument | Can access instance data? | Can access class data? | Common use |
|---|---|---|---|---|
| Instance method | self | Yes | Yes | Work with one object |
| Class method | cls | Not directly | Yes | Alternative constructors, class-level behavior |
| Static method | None | No | No, unless referenced manually | Utility logic related to the class |
@classmethod vs @staticmethod
Cheat Sheet
Quick rules
- Instance method: first parameter is
self - Class method: use
@classmethod, first parameter iscls - Static method: use
@staticmethod, no automatic first parameter
Syntax
class Example:
def instance_method(self):
pass
@classmethod
def class_method(cls):
pass
@staticmethod
def static_method(x, y):
return x + y
When to use which
- Use instance methods for object-specific behavior
- Use class methods for class-level behavior or alternative constructors
- Use static methods for helper logic that belongs with the class
Memory tip
self= this objectcls= this class
FAQ
What is the main difference between @staticmethod and @classmethod in Python?
@classmethod receives the class as the first argument, usually named cls. @staticmethod receives no automatic first argument.
When should I use @classmethod in Python?
Use it when the method needs the class itself, especially for alternative constructors like from_dict, from_json, or default.
When should I use @staticmethod in Python?
Use it for helper functions that belong to the class logically but do not need instance or class data.
Can I call a class method on an instance?
Yes. You can call both class methods and static methods from either the class or an instance.
Can a static method access class variables?
Not automatically. It does not receive cls, but it can still refer to the class by name directly if needed.
Is @staticmethod the same as a normal function?
Not exactly. It behaves similarly because it gets no automatic arguments, but it is stored inside the class namespace.
Mini Project
Description
Build a small User class that demonstrates all three method styles: an instance method, a class method, and a static method. This project shows a realistic pattern where you validate input with a static method, create objects from external data with a class method, and display object data with an instance method.
Goal
Create a User class that can be built from a dictionary, validate email text, and describe a specific user instance.
Requirements
[ "Create a User class with name and email attributes", "Add an instance method that returns a readable description of the user", "Add a class method that creates a User from a dictionary", "Add a static method that checks whether an email contains an @ symbol", "Use all three methods in example code" ]
Keep learning
Related questions
Catch Multiple Exceptions in One except Block in Python
Learn how to catch multiple exceptions in one Python except block using tuples, with examples, mistakes, and real-world usage.
Convert Bytes to String in Python 3
Learn how to convert bytes to str in Python 3 using decode(), text mode, and proper encodings with practical examples.
Does Python Have a Ternary Conditional Operator? Conditional Expressions in Python
Learn whether Python has a ternary operator, how conditional expressions work, and when to use them with clear Python examples.