In Python, there are two ways to create a Static Method, i.e., using the static method () method and the @staticmethod decorator.
This blog will explain the creation of static methods with these two approaches and provide examples of how to understand them.
Table of Contents:
Python Static Method using @staticmethod decorator
A Static method is a method that belongs to the class rather than an instance of the class. It does not take self or cls as the first argument. Static methods are defined using the @staticmethod decorator
Key points about static methods:
- No access to instance (self) or class (cls): Static methods cannot access or modify instance-specific data or class-specific data. They behave just like regular functions, but they are associated with the class for organizational purposes.
- Defined using @staticmethod decorator: Static methods are defined using the @staticmethod decorator, followed by the method definition.
- Called on the class or instance: Static methods can be called on the class itself or an instance of the class.
Syntax:
class MyClass:
@staticmethod
def my_static_method(arg1, arg2, ...):
print(f"...")
Example: This example describes the use of the @staticmethod decorator in Python.
class Calculator:
@staticmethod
def add(x, y):
return x + y
@staticmethod
def multiply(x, y):
return x * y
# Calling static methods on the class
result_add = Calculator.add(4, 8)
result_multiply = Calculator.multiply(4, 8)
print(f"Sum: {result_add}, Product: {result_multiply}")
Output:

Python Static Method using staticmethod() function
The staticmethod() is a standard library function in Python that is used to create a static method. It accepts a method as an argument and converts it into a static method. It is useful for utility functions that perform tasks related to the class but don’t need to access or modify its attributes.
Syntax
class MyClass:
@staticmethod
def my_static_method(param1, param2):
# Method implementation
print(param1, param2)
Example: This example describes the use of the staticmethod() function in Python
class Calculator:
def add(x, y):
return x + y
def multiply(x, y):
return x * y
# Manually convert methods to static methods
add = staticmethod(add)
multiply = staticmethod(multiply)
# Calling static methods on the class
result_add = Calculator.add(5, 3)
result_multiply = Calculator.multiply(5, 3)
print(f"Sum: {result_add}, Product: {result_multiply}")
Output:

When to Use
Use @staticmethod decorator:
- When you are writing a class and want to clearly define a static method.
- It is more readable and the standard approach in Python.
Use staticmethod() function:
- If you’re working with code that dynamically creates or modifies methods, or if you want to convert a function to a static method after the function is already defined.
Conclusion
In Python, both the @staticmethod decorator and the staticmethod() function are used to define static methods, which belong to the class rather than instances. The @staticmethod decorator is the preferred and more common approach due to its simplicity and clarity when defining static methods. On the other hand, staticmethod() offers flexibility for dynamically converting functions into static methods, though it is less commonly used. Both methods serve the same purpose but the decorator is generally the more Pythonic and readable choice.
FAQ’s
1. What is a static method?
A static method is a method that belongs to the class, not an instance, and doesn’t access instance or class data.
2. What’s the difference between @staticmethod and staticmethod()?
@staticmethod is a decorator for simplicity, while staticmethod() is a function that can convert a regular method into a static method manually.
3. When to use @staticmethod Decorator?
Use @staticmethod when you want a clear, concise way to define static methods.
4. Can static methods access instance or class variables?
No, static methods cannot access instance (self) or class (cls) variables.
5. Can class methods and static methods be used interchangeably?
No, class methods and static methods cannot be used interchangeably. Class methods operate on class-level data, while static methods do not access class or instance data.