What Is a Python Static Method? How to Use It

Defining a method in a Python class means choosing what the function receives when it is called. A static method is a function on the class that receives only the arguments you pass, with no automatic self or cls value.

This article shows how to define one with @staticmethod and compare calls through the class and an instance.

TL;DR

A Python static method is a class function that receives only the arguments you pass, without an automatic self or cls value.

  • Define one with @staticmethod above the function.
  • Call it through the class when the function needs no object or class state.
  • Choose an instance method for object state, a class method for class-specific behavior, or a module function for a general helper.

What Is a Python Static Method?

A staticmethod descriptor controls lookup of the class attribute and returns its function without binding self or cls. An ordinary function accessed on an instance receives the instance as its first argument, while a classmethod receives the accessing class.

I call Text.clean by class name because the helper describes Text rules rather than one Text object. The function can read a class attribute through an explicit class name, but that name stays fixed when a subclass calls it.

Method formAutomatic first argumentChoose it when
Instance methodself, the instanceThe operation reads or changes one object’s state
Class methodcls, the accessing classThe operation uses class data or creates the right subclass
Static methodNoneThe operation needs only its named inputs

Use @staticmethod above a definition or pass an existing function to staticmethod() inside the class body.

A static method may declare zero parameters if it needs no input. Python 3.10 also made the descriptor itself callable and added wrapped-function metadata, but the usual call still goes through the class attribute.

How to Define and Call a Static Method Step by Step

Text.clean strips surrounding whitespace and lowercases the supplied string. I kept the example in one file because the same command shows instance binding and inheritance.

Step 1: Mark the function with @staticmethod

Keep parameters for values the function actually needs. PlainText has no decorator or self parameter, so its call shows what ordinary function binding adds. Rule and ChildRule test inheritance, while StrictRule overrides the validator.

class Text:
    @staticmethod
    def clean(value):
        return value.strip().lower()


class PlainText:
    def clean(value):
        return value.strip().lower()


class Rule:
    @staticmethod
    def valid(value):
        return value.startswith("REF-")


class ChildRule(Rule):
    pass


class StrictRule(Rule):
    @staticmethod
    def valid(value):
        return value.startswith("REF-") and value.endswith("-OK")


print(Text.clean("  Python  "))
print(Text().clean("  Python  "))
print(PlainText.clean("Python"))

try:
    PlainText().clean("Python")
except TypeError as error:
    print(f"{type(error).__name__}: {error}")

print(Rule.valid("REF-42-NO"))
print(ChildRule.valid("REF-42-NO"))
print(StrictRule.valid("REF-42-NO"))

Step 2: Run the file and compare the calls

Run python3 staticmethod_demo.py from the saved file. I ran that command and got python three times, the extra-argument TypeError, then True, True, and False for the base rule, inheriting child, and override.

python
python
python
TypeError: PlainText.clean() takes 1 positional argument but 2 were given
True
True
False
Terminal output from Python static method calls and an instance binding error
The terminal shows three successful calls, the extra-argument TypeError, and inherited and overridden results.

Text.clean returns python from class and instance access, and PlainText.clean also returns python when called through its class.

Step 3: Choose a home for a stateless helper

Rule.valid checks a candidate string beside that type’s validation rules, and callers can invoke it through the class without creating a Rule object.

Rule.valid accepts references beginning with REF-. StrictRule adds the -OK suffix requirement when that ending is part of the input contract.

The boolean result can act as a validation check before a caller constructs an object to store the reference. Rule.valid leaves that object lifecycle to the caller. A helper shared across unrelated code fits at module level, where its name does not imply a single class relationship.

Static Method Edge Cases and When to Choose Another Form

I kept the caught TypeError in the example because its text names the function and the extra positional argument.

CaseWhat happensWhat to choose
An undecorated one-parameter function is called through an instancePython inserts the instance, so the function receives two valuesAdd @staticmethod when object state is irrelevant, or declare self when it is used
The helper needs class configuration or an alternate constructorA static method has no automatic cls value. Inherited factories need the class that made the callUse @classmethod and refer to cls
A singleton accessor creates or returns an instanceOne controlled creation path must store and reuse the instanceUse @classmethod when construction must follow the calling subclass
The helper applies outside one classClass placement adds a namespace but no class-specific behaviorKeep a reusable helper at module scope
A subclass replaces a static methodLookup selects the subclass definition, but the function gets no subclass valueUse a class method when the implementation needs to inspect the accessing class

If callers do not need the same instance later, a regular constructor keeps object creation local.

A static method can still mutate an explicit list or global state, so inspect its body when side effects matter. The Python descriptor guide explains the lookup difference. AskPython’s classmethod guide and self explanation cover the other bindings.

Conclusion

The function signature shows what Python supplies at call time, while the method form tells you which class or object data is available. For reference, see the Python staticmethod reference and AskPython’s classes and objects guide.

FAQ

These answers cover syntax and method selection questions that remain after the example.

Can a static method have no parameters?

Yes. It receives no implicit self or cls argument, and its function can declare zero or more explicit parameters.

Can I use staticmethod() without the @ syntax?

Yes. Pass a function to staticmethod(), for example by assigning method = staticmethod(function) in a class body. The @staticmethod form is the usual way to make that binding clear beside the definition.

Should a factory method be static?

Use @classmethod when a factory should construct the class used for the call. Its cls argument lets an inherited factory create the appropriate subclass.

Pankaj Kumar
Pankaj Kumar

I have been working on Python programming for more than 12 years. At AskPython, I share my learning on Python with other fellow developers.

Articles: 261