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

ParameterDescription
function The function to wrap (the decorator passes it for you).

Return value

A static method object.

Examples

A helper that belongs with the classTry it yourself
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
Group a utility function with its classTry it yourself
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

VersionChange
Python 3.0Added
Python 3.10Static methods can be called as regular functions and copy the wrapped function's attributes.