Seeing @staticmethod in Python code can raise a basic question: why doesn’t the method have a self parameter, which refers to the current instance? The decorator marks a method that receives no instance or class argument automatically when called.

I’ll show how to define and call a static method, then explain when it fits better than an instance or class method.

TL;DR

@staticmethod defines a method that receives no automatic self or cls argument, so call it with the inputs it needs.

  • Call it as Class.method(arguments).
  • Use it for a function that belongs beside a class but does not read its instance or class state.
  • Use an instance method for object state and a class method for class-aware behavior.

What is @staticmethod in Python?

When Python executes a class body, it creates a namespace containing the class attributes. A descriptor can define how an attribute behaves when accessed.

Python’s built-in staticmethod() decorator stores a function so attribute access does not bind an instance or class as its first argument. This makes a method useful for a rule that belongs beside a class’s other operations but does not need its state.

How to define and call a static method step by step

A unit-conversion helper can accept celsius and return a Fahrenheit value. The signature names the input value and does not reserve a first parameter for self or cls.

Because each unit appears in the parameter name or method name, a call such as Temperature.to_fahrenheit(20) makes the conversion direction apparent. The calculation uses the supplied number rather than a Temperature object. If the conversion depends on a scale configured on the class, a class method makes that dependency available through cls.

Step 1: Define the function with explicit inputs

The built-in staticmethod() can also wrap a function directly, though decorator syntax makes the behavior visible beside the definition. The function keeps its normal parameter list and returns whatever its body specifies.

class Text:
    prefix = "Result:"

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


class LabeledText(Text):
    prefix = "Label:"


print(Text.clean("  Hello Python  "))
print(LabeledText.clean("  Hello Python  "))
print(Text.clean)
print(Text().clean("  Hello Python  "))

Step 2: Call it through the class

I ran the complete example with python3 staticmethod_demo.py. The class and subclass calls both normalize the explicit string.

Step 3: Check instance access and inheritance

The last printed line makes the call on a newly created Text instance.

hello python
hello python

hello python

The address printed for the function can vary between runs and is not a useful value for application code.

When should you use @staticmethod?

Use a small set of design checks before placing a helper on a class:

  • Give conversions parameter names that make units visible.
  • Decide whether validation returns a result or raises an error.
  • Keep normalization from changing the caller’s original object.
Task Example signature Why this can fit
Convert Celsius to Fahrenheit to_fahrenheit(celsius) Both units are explicit values
Convert seconds to minutes to_minutes(seconds) The conversion uses the supplied duration
Check whether a string is numeric is_number(value) The answer depends on the supplied value
Normalize a label normalize(text) The transformation can be stated as a standalone rule

Static describes how Python binds the function, not whether its inputs are immutable. A static method can accept a mutable object, so its body still needs ordinary care about side effects.

Since Python 3.10, a staticmethod object can also be called directly and carries the wrapped function’s metadata. This version change matters when code inspects the descriptor itself rather than calling the method through its class.

Static method vs class method vs instance method

Each method form communicates what the caller supplies and what context the implementation can use. The table summarizes the signatures side by side.

Method form Implicit first argument Typical call Use it when
Instance method self, the instance item.describe() Behavior depends on object state
Class method cls, the accessing class Item.from_text(text) Behavior depends on class state or constructs that class
Static method None Item.clean(text) Only explicit inputs matter

What edge cases should you check?

Check whether a subclass needs to change the helper’s behavior and whether the helper belongs on the class at all. Those decisions keep the method’s call behavior clear.

Case What to check Choice
A subclass needs class-specific behavior An inherited static method receives no subclass argument Use a class method when the implementation needs the accessing class through cls
The helper has no meaningful link to the class Class placement adds no useful context Use a module-level function instead

Conclusion

Consult the Python staticmethod documentation for descriptor details. The linked AskPython guides cover classmethod and self.

FAQ

These short answers cover the common call and design questions.

How do you call a static method in Python?

Call it through the class, such as Text.clean(value), or through an instance. Python passes no implicit self or cls argument.

When should you use a static method?

Use one when a function belongs logically with a class but needs only its explicit arguments, not instance or class state.

Are static methods inherited in Python?

Yes. A subclass inherits the method attribute and can override it by defining the same name. An inherited static method does not receive the subclass as an implicit argument.

Share.
Leave A Reply