Python 3 reference · Built-in Functions
Python staticmethod() Function
Turn a method into a static method that receives neither the instance nor the class. Usually written as @staticmethod.
Python 3 Since 3.0
Syntax
@staticmethod
def method(...): ...
Parameters
| Parameter | Description |
|---|---|
function |
The function to wrap (the decorator passes it for you). |
Return value
A static method object.
Examples
class Temperature:
@staticmethod
def c_to_f(celsius):
return celsius * 9 / 5 + 32
print(Temperature.c_to_f(100))
print(Temperature().c_to_f(0))
Output
212.0 32.0
class Password:
@staticmethod
def is_strong(text):
return len(text) >= 8 and any(c.isdigit() for c in text)
print(Password.is_strong("abc"), Password.is_strong("secret123"))
Output
False True
Good to know
A static method is a plain function placed in a class for organisation. If it needs the class, use
@classmethod; if it needs nothing from the class, a module-level function is often simpler.
Version history
| Version | Change |
|---|---|
| Python 3.0 | Added |
| Python 3.10 | Static methods can be called as regular functions and copy the wrapped function's attributes. |
Related
classmethod()· Turn a method into a class method that receives the class (cls) instead of an instance. Usually written as @classmethod.property()· Turn methods into a managed attribute with a getter, and optional setter and deleter. Usually written as @property.- Python Classes and Objects · tutorial