In Python Object-Oriented Programming (OOP), static methods are methods that belong to a class but do not access or modify class or instance data. They work like regular functions but are placed inside a class because they are logically related to that class.
Python provides the @staticmethod decorator to create static methods.
In this tutorial, you will learn what static methods are, how they work, and when to use them.
What is a Static Method?
A static method is a method that:
- Belongs to a class
- Does not use
self - Does not use
cls - Cannot directly access instance attributes
- Cannot directly access class attributes
Static methods are mainly used for utility functions related to the class.
Why Use Static Methods?
Static methods are useful when:
- The method performs a task related to the class
- No object data is needed
- No class data is needed
- You want to group utility functions inside a class
Creating a Static Method
Syntax
class ClassName:
@staticmethod
def method_name():
passYour First Static Method
Example
class Calculator:
@staticmethod
def greet():
print("Welcome to Calculator")
Calculator.greet()Output
Welcome to CalculatorStatic Method Without Creating Objects
Unlike instance methods, static methods can be called directly using the class name.
Example
class Utility:
@staticmethod
def show_message():
print("Utility Function")
Utility.show_message()Calling Static Methods Using Objects
Static methods can also be called using objects.
Example
class Demo:
@staticmethod
def display():
print("Static Method Called")
obj = Demo()
obj.display()Output
Static Method CalledStatic Method vs Instance Method
Instance Method
class Student:
def show(self):
print("Instance Method")Uses:
student = Student()
student.show()Static Method
class Student:
@staticmethod
def show():
print("Static Method")Uses:
Student.show()Static Method vs Class Method
Static Method
class Test:
@staticmethod
def show():
print("Static")- No
self - No
cls
Class Method
class Test:
@classmethod
def show(cls):
print("Class Method")- Uses
cls - Accesses class attributes
Difference Between Method Types
| Feature | Instance Method | Class Method | Static Method |
|---|---|---|---|
| Uses self | Yes | No | No |
| Uses cls | No | Yes | No |
| Access Object Data | Yes | No | No |
| Access Class Data | Yes | Yes | No |
| Decorator | None | @classmethod | @staticmethod |
Real-World Example: Calculator
Static methods are commonly used in calculator applications.
Example
class Calculator:
@staticmethod
def add(a, b):
return a + b
@staticmethod
def subtract(a, b):
return a - b
print(Calculator.add(10, 5))
print(Calculator.subtract(10, 5))Output
15
5Real-World Example: Temperature Converter
class Converter:
@staticmethod
def celsius_to_fahrenheit(c):
return (c * 9/5) + 32
print(Converter.celsius_to_fahrenheit(25))Output
77.0Real-World Example: Validation System
class Validator:
@staticmethod
def is_positive(number):
return number > 0
print(Validator.is_positive(10))
print(Validator.is_positive(-5))Output
True
FalseUsing Static Methods Inside Classes
A static method can be called from another method.
Example
class MathTool:
@staticmethod
def square(num):
return num * num
def show_square(self, num):
print(MathTool.square(num))
tool = MathTool()
tool.show_square(5)Output
25Common Mistakes
Mistake 1: Using self Inside Static Method
❌ Wrong
class Test:
@staticmethod
def show():
print(self.name)✔ Static methods do not use self
Mistake 2: Using cls Inside Static Method
❌ Wrong
class Test:
@staticmethod
def show():
print(cls.value)✔ Static methods do not use cls
Mistake 3: Forgetting @staticmethod
❌ Wrong
class Test:
def show():
print("Hello")✔ Correct
class Test:
@staticmethod
def show():
print("Hello")Best Practices
- Use static methods for utility functions.
- Do not access object data inside static methods.
- Use meaningful method names.
- Call static methods using the class name whenever possible.
Practical Example: Geometry Helper
class Geometry:
@staticmethod
def area_square(side):
return side * side
@staticmethod
def perimeter_square(side):
return side * 4
print(Geometry.area_square(6))
print(Geometry.perimeter_square(6))Output
36
24Static Methods Summary
| Purpose | Example |
| Utility Functions | Calculator |
| Converters | Temperature Converter |
| Validators | Email Validation |
| Mathematical Operations | Geometry Calculations |
| Helper Functions | Data Processing |
Conclusion
Static methods are an important feature of Python OOP that allow you to place utility functions inside a class without requiring access to object or class data.
You learned:
- What static methods are
- How to create them using
@staticmethod - Differences between instance, class, and static methods
- Real-world use cases
- Best practices for static methods
Understanding static methods will help you write cleaner, more organized, and more professional Python applications.
Practice Exercises
Exercise 1
Create a class named MathOperations with static methods:
- add()
- subtract()
- multiply()
Exercise 2
Create a class named Converter with static methods:
- kilometers_to_miles()
- miles_to_kilometers()
Exercise 3
Create a class named Validator with a static method that checks whether a number is even or odd.


0 Comments