understanding static method in python.
Why Use a Static Method?
A very common question is:
If an instance method works, why should I use a static method?
Let's understand this using the Calculator example.
Version 1: Instance Method
class Calculator:
def add(self, a, b):
return a + b
c = Calculator()
print(c.add(10, 5))
Output
15
This works perfectly.
But let's see what Python does internally.
What Happens Internally?
When you write:
c.add(10, 5)
Python converts it to:
Calculator.add(c, 10, 5)
So:
self = c
a = 10
b = 5
Even though the method never uses:
self
Python still passes the object.
The Problem
Look carefully:
class Calculator:
def add(self, a, b):
return a + b
Inside the method we use:
a
b
But we never use:
self
The object serves no purpose.
A Useful Question
Ask yourself:
Does this method need anything from the object?
self.name
self.age
self.balance
self.history
If the answer is No, then an instance method may not be the best choice.
Version 2: Static Method
class Calculator:
@staticmethod
def add(a, b):
return a + b
Usage:
print(Calculator.add(10, 5))
Output
15
What Happens Internally?
Python simply executes:
add(10, 5)
No object.
No self.
No cls.
Only the required arguments.
Why Static Method Is Better Here
The method:
add(a, b)
needs:
a
b
It does NOT need:
self
It does NOT need:
cls
Therefore:
Static Method
is a better design.
Visual Comparison
Instance Method
Calculator Object
│
▼
add(self, a, b)
│
▼
Uses only a and b
Notice:
Object was passed
but never used.
Static Method
add(a, b)
│
▼
Uses only a and b
No unnecessary object.
Another Example
Suppose we create:
class Calculator:
@staticmethod
def multiply(a, b):
return a * b
Usage:
print(Calculator.multiply(5, 4))
Output:
20
Again:
a
b
are enough.
No object data required.
When Instance Method Becomes Necessary
Now suppose the calculator keeps history.
class Calculator:
def __init__(self):
self.history = []
def add(self, a, b):
result = a + b
self.history.append(result)
return result
Usage:
c = Calculator()
c.add(10, 5)
c.add(20, 5)
print(c.history)
Output:
[15, 25]
Why Can't This Be Static?
Because the method uses:
self.history
which belongs to the object.
Without:
self
Python wouldn't know which calculator's history to update.
Real-Life Analogy
Static Method
Think of:
2 + 3
The answer is always:
5
No memory required.
No object required.
A static method is perfect.
Instance Method
Think of a calculator that stores previous calculations.
Calculator A
History:
5
10
20
Calculator B
History:
100
200
Now each calculator has its own data.
An instance method is required.
Common Good Uses for Static Methods
Example 1: Temperature Conversion
class Temperature:
@staticmethod
def celsius_to_fahrenheit(c):
return (c * 9/5) + 32
Usage:
Temperature.celsius_to_fahrenheit(25)
No object needed.
Example 2: Email Validation
class Validator:
@staticmethod
def is_valid_email(email):
return "@" in email
Usage:
Validator.is_valid_email("[email protected]")
No object data needed.
Example 3: Math Utilities
class MathUtils:
@staticmethod
def square(number):
return number * number
Usage:
MathUtils.square(5)
Output:
25
Bad Candidate for Static Method
class Student:
def __init__(self, name):
self.name = name
def show_name(self):
return self.name
Why?
Because:
self.name
belongs to the object.
A static method cannot access it.
Decision Tree
Does the method need object data?
│
├─ YES
│ ↓
│ Instance Method
│
└─ NO
│
├─ Need class data?
│ ↓
│ Class Method
│
└─ No object data
No class data
↓
Static Method
Rule to Remember
Instance Method
↓
Uses self
Class Method
↓
Uses cls
Static Method
↓
Uses neither self nor cls
Final Takeaway
The original Calculator example works as an instance method:
class Calculator:
def add(self, a, b):
return a + b
But since the method never uses:
self
a better design is:
class Calculator:
@staticmethod
def add(a, b):
return a + b
because:
- No object data is needed.
- No class data is needed.
- The method is simply a utility function.
- Static methods clearly communicate that the operation is independent of any object.
Instance Method vs Class Method vs Static Method
This is one of the most important OOP concepts.
The easiest way to understand it is:
| Method Type | Works With | First Parameter | Decorator |
|---|---|---|---|
| Instance Method | Object Data | self |
None |
| Class Method | Class Data | cls |
@classmethod |
| Static Method | Independent Utility Function | None | @staticmethod |
One Example Showing All Three
class Student:
# Class Variable
school = "ABC School"
def __init__(self, name):
self.name = name
# Instance Method
def show_name(self):
return self.name
# Class Method
@classmethod
def get_school(cls):
return cls.school
# Static Method
@staticmethod
def add(a, b):
return a + b
1. Instance Method
Definition
Instance methods work with object data.
They receive:
self
which refers to the current object.
Example
class Student:
def __init__(self, name):
self.name = name
def show_name(self):
return self.name
Usage:
s1 = Student("John")
print(s1.show_name())
Output:
John
What Happens Internally?
s1.show_name()
becomes:
Student.show_name(s1)
So:
self = s1
When to Use
Whenever you need object-specific data.
Examples:
self.name
self.age
self.salary
self.balance
2. Class Method
Definition
Class methods work with class data.
They receive:
cls
which refers to the class.
Example
class Student:
school = "ABC School"
@classmethod
def get_school(cls):
return cls.school
Usage:
print(Student.get_school())
Output:
ABC School
What Happens Internally?
Student.get_school()
becomes:
Student.get_school(Student)
So:
cls = Student
When to Use
Whenever you need class-level data.
Examples:
school
company_name
tax_rate
country
that are shared by all objects.
3. Static Method
Definition
Static methods do not work with:
self
or
cls
They are just regular functions placed inside a class.
Example
class Math:
@staticmethod
def add(a, b):
return a + b
Usage:
print(Math.add(10, 5))
Output:
15
What Happens Internally?
Nothing special.
Python does NOT pass:
self
or
cls
The method simply receives:
a = 10
b = 5
When to Use
When the function logically belongs to the class but doesn't need object data or class data.
Examples:
calculate_tax()
validate_email()
convert_temperature()
add_numbers()
Visual Representation
Student Class
│
├── school = "ABC School" ← Class Variable
│
├── show_name() ← Instance Method
│ ▲
│ │
│ self
│
├── get_school() ← Class Method
│ ▲
│ │
│ cls
│
└── add() ← Static Method
▲
│
No self
No cls
Rule to Remember
Need object data?
↓
Use Instance Method (self)
Need class data?
↓
Use Class Method (cls)
Need neither?
↓
Use Static Method
Quick Examples
# Instance Method
def show_name(self):
# Class Method
@classmethod
def get_school(cls):
# Static Method
@staticmethod
def add(a, b):
Mental Model
Instance Method → Object
Class Method → Class
Static Method → Neither (just a helper function inside the class)