In Python, @staticmethod marks a method that is just a plain function stored in a class: it does not receive self (the instance) or cls (the class). You can call it on the class or on an instance with exactly the arguments you pass.
Use it for helper functions that belong with a class logically but need no instance or class data, such as unit conversions or validation. If the helper needs the class, use classmethod; if it needs object data, use a normal method.
staticmethod is a built-in type (a class) that you call like a function: each call creates a new staticmethod object. Like every built-in, it lives in the builtins module and is always available without an import.
Syntax
Usually written as a decorator.
python
class MyClass:
@staticmethod
def helper(x, y):
return x + y
Call
What it does
MyClass.helper(1, 2)
Called on the class.
obj.helper(1, 2)
Also works; no self is passed.
staticmethod(func)
The non-decorator form.
self.helper(x)
Call it from other methods.
A module-level function is often just as good; choose a static method when the function clearly belongs to the class's namespace.
Library declarations
python
staticmethod(function, /)
# class staticmethod: usually used as @staticmethod
Parameters
Parameter
Description
function
Required. A function; it will be called without any automatic first argument.
Return Value
Return Value
Description
staticmethod
A static method object to store in the class body.
Python Examples
Example 1 — Utility Functions Grouped in a Class
Call two static helpers on the class and on an instance.
python
class Units:
@staticmethod
def km_to_m(km):
return km * 1000
@staticmethod
def is_valid_distance(value):
return value >= 0
print(Units.km_to_m(2.5))
print(Units().km_to_m(3))
print(Units.is_valid_distance(-1))
Output
2500.0
3000
False
How It Works
No self.km_to_m takes only the number it needs.
Call on the class.Units.km_to_m(2.5) needs no object.
Or on an instance.Units().km_to_m(3) passes nothing extra.
Grouped helpers. Related conversions live in one namespace.
Example 2 — Instance, Class, and Static Methods Together
See which first argument each kind of method receives.