Static Method in Python: A Complete Guide to @staticmethod

If you are learning object-oriented programming in Python, you will eventually come across the static method in Python. At first, @staticmethod can seem confusing because it creates a method inside a class without using the familiar self parameter. But once you understand what Python is actually doing, the concept becomes surprisingly simple. A static method is essentially a function that lives inside a class for organizational or logical reasons but does not need information from a particular object or the class itself. Python’s official documentation confirms that a static method does not receive an implicit first argument and can be called through either the class or an instance.

The easiest way to think about it is this: an instance method is behavior connected to an object, while a static method is a useful operation connected to the idea represented by the class. For example, an Employee class might contain employee-specific methods that use self.name or self.salary, while a static method might validate whether a salary is positive. The validation relates to employees, but it doesn’t need a particular employee object. That distinction is the key to understanding when and why you should use @staticmethod.

What Is a Static Method in Python?

A static method in Python is a method defined inside a class using the @staticmethod decorator. Unlike a normal instance method, it does not automatically receive self. Unlike a class method, it does not automatically receive cls. Python simply makes the underlying function available through the class namespace without binding an instance or class as the first argument.

Here is a basic example:

class Calculator:

    @staticmethod
    def add(a, b):
        return a + b

You can call this method directly through the class:

result = Calculator.add(10, 5)

print(result)

Output:

15

Notice that there is no self parameter. That’s because add() doesn’t need information about a Calculator object. It simply receives two values and performs an operation. Python’s documentation describes this behavior directly: a static method receives no implicit first argument.

This makes static methods particularly useful for small utility operations that logically belong to a class but are independent of individual objects.

How Does @staticmethod Work?

The @staticmethod syntax is a decorator. It changes how Python handles the function when that function is defined inside a class. Normally, when you access an ordinary function through an instance, Python binds the instance to it as the first argument. This is why instance methods normally use self.

For example:

class Person:

    def greet(self):
        return "Hello!"

When you write:

person = Person()
person.greet()

Python supplies person as the first argument.

A static method prevents that automatic binding:

class Person:

    @staticmethod
    def greet():
        return "Hello!"

Now both of these work:

Person.greet()

and:

person = Person()
person.greet()

Python’s descriptor documentation explains that static methods return the underlying function without adding an instance or class argument.

This is why you should not add self to a static method unless you deliberately want self to be an ordinary parameter. Python will not automatically provide an object for it.

Python Static Method Syntax

The syntax is simple:

class ClassName:

    @staticmethod
    def method_name(arguments):
        # method code
        pass

For example:

class Temperature:

    @staticmethod
    def celsius_to_fahrenheit(celsius):
        return (celsius * 9 / 5) + 32

You can use it like this:

temperature = Temperature.celsius_to_fahrenheit(25)

print(temperature)

Output:

77.0

The method doesn’t need a Temperature object because the calculation only depends on the value supplied to it.

Python also allows the underlying staticmethod() constructor to be used directly, although the decorator form is much more readable in normal application code. The official documentation shows both approaches.

Static Method vs Instance Method

Understanding the difference between a static method and an instance method is essential.

An instance method works with an individual object and normally receives self:

class Student:

    def __init__(self, name):
        self.name = name

    def introduce(self):
        return f"My name is {self.name}"

Here, introduce() needs self because it accesses the student’s name.

Now consider:

class Student:

    @staticmethod
    def school_rule():
        return "Students must arrive on time."

The school_rule() method doesn’t depend on any particular student. It doesn’t need self.name or any other instance attribute.

The difference can be summarized like this:

FeatureInstance MethodStatic Method
Uses selfYesNo
Receives instance automaticallyYesNo
Accesses instance attributesYesNo
Uses @staticmethodNoYes
Can be called through classYes, with normal binding rulesYes
Can be called through instanceYesYes
Main purposeObject-specific behaviorIndependent related functionality

Python’s data model describes ordinary functions becoming bound methods when accessed through instances, while static methods prevent that transformation.

A useful question to ask yourself is: Does this method need information from a particular object? If the answer is yes, you probably want an instance method. If the answer is no, a static method might be appropriate.

Static Method vs Class Method

Another common source of confusion is the difference between @staticmethod and @classmethod.

A class method receives the class as an implicit first argument, normally named cls. A static method receives neither an instance nor a class automatically. Python’s documentation specifically distinguishes these two decorators.

Consider this example:

class Employee:

    company = "ABC Ltd"

    @classmethod
    def get_company(cls):
        return cls.company

    @staticmethod
    def add_bonus(salary, bonus):
        return salary + bonus

get_company() uses cls.company, so it needs access to the class. That’s why it is a class method.

add_bonus() only needs two values. It doesn’t care about the class or an employee object, so it can be static.

Method TypeAutomatic ArgumentTypical Purpose
Instance methodselfWork with an object
Class methodclsWork with class-level data
Static methodNoneIndependent utility related to the class

This simple comparison solves many of the questions beginners have about Python methods.

Practical Examples of Static Methods

One of the most common uses of static methods is validation.

class User:

    @staticmethod
    def is_valid_username(username):
        return len(username) >= 3

You can call:

print(User.is_valid_username("Ali"))

Output:

True

The method doesn’t need a User object. It simply checks the supplied value.

Another common use is mathematical or conversion logic:

class MathTools:

    @staticmethod
    def square(number):
        return number ** 2

Then:

print(MathTools.square(8))

produces:

64

You can also use static methods for formatting:

class DateFormatter:

    @staticmethod
    def format_date(day, month, year):
        return f"{day:02d}/{month:02d}/{year}"

Calling:

print(DateFormatter.format_date(5, 8, 2026))

produces:

05/08/2026

In each example, the method has a conceptual relationship with the class, but it doesn’t need an instance.

When Should You Use a Static Method?

The best time to use a static method is when a function is logically related to a class but independent of its instance and class state.

Imagine a Product class:

class Product:

    def __init__(self, name, price):
        self.name = name
        self.price = price

You might add a method for calculating a discount:

class Product:

    def __init__(self, name, price):
        self.name = name
        self.price = price

    @staticmethod
    def discounted_price(price, percentage):
        return price - (price * percentage / 100)

The discount calculation doesn’t need self.name or self.price. It only needs the values supplied to it.

You could call:

price = Product.discounted_price(1000, 10)

print(price)

Output:

900.0

This is a reasonable use because the operation is closely associated with products.

However, don’t automatically put every helper function into a static method. Python’s own programming FAQ points out that a simple module-level function can sometimes be a more straightforward alternative.

Advantages of Static Methods

Static methods provide several practical benefits. First, they clearly communicate that a method doesn’t depend on object state. When another developer sees @staticmethod, they immediately know that self isn’t expected.

They can also improve organization. A function that specifically relates to a class can remain grouped with that class instead of being scattered across unrelated parts of a project. For example, Product.calculate_discount() communicates more context than a generic function name such as calculate_discount().

Static methods are also convenient for testing because they don’t require an instance to be created simply to run an independent operation. A pure calculation or validation function can usually be tested directly with different inputs.

Modern Python versions also preserve method metadata such as __name__, __doc__, and annotations for static methods. This behavior was enhanced in Python 3.10.

The biggest benefit, however, is clear code organization. A good static method tells the reader, “This operation belongs conceptually here, but it doesn’t need an object.”

Common Mistakes With @staticmethod

One common mistake is trying to use self inside a static method:

class User:

    @staticmethod
    def greet():
        return self.name

This fails because there is no automatically supplied self.

If the method needs an object’s attributes, make it an instance method:

class User:

    def __init__(self, name):
        self.name = name

    def greet(self):
        return self.name

Another mistake is creating static methods for everything. Just because a method doesn’t use self doesn’t automatically mean it should be static. If the function doesn’t have a meaningful relationship with the class, keeping it as a normal module-level function can make the code easier to understand.

You should also avoid confusing static methods with class methods. If your method needs cls to access class-level information or create an instance based on the current class, @classmethod is usually the better choice.

Static Method Best Practices

When writing static methods, keep them small, focused, and predictable. A method that validates an email address or converts a temperature is easy to understand. A static method that performs ten unrelated operations is not.

Use descriptive names:

@staticmethod
def is_valid_email(email):
    ...

is much clearer than:

@staticmethod
def check(value):
    ...

Type hints can also make your code easier to understand:

class Calculator:

    @staticmethod
    def multiply(a: float, b: float) -> float:
        return a * b

Add a docstring when the behavior isn’t immediately obvious:

class PriceTools:

    @staticmethod
    def apply_discount(price: float, percentage: float) -> float:
        """Return the price after applying a percentage discount."""
        return price - (price * percentage / 100)

Most importantly, don’t use @staticmethod simply because you can. Python’s programming FAQ explicitly notes that a regular module-level function may sometimes provide the same effect more straightforwardly.

Frequently Asked Questions

What is a static method in Python?

A static method is a function defined inside a class using @staticmethod. It doesn’t automatically receive self or cls and is generally used for functionality related to a class that doesn’t require instance or class state.

How do you call a static method?

You can call it directly through the class:

Calculator.add(5, 10)

Python also allows a static method to be accessed through an instance.

Does a static method use self?

No. A static method doesn’t receive an implicit first argument. If you write self in its parameter list, Python treats it as an ordinary parameter rather than automatically supplying an instance.

What is the difference between static and class methods?

A class method receives cls automatically and can work with class-level information. A static method receives neither self nor cls automatically and is generally used for independent functionality related to the class.

Should I always use a static method for functions without self?

No. If a function has no meaningful relationship with the class, a normal module-level function may be a cleaner choice. Static methods are primarily useful when keeping the operation inside the class improves organization or communicates its relationship to the class.

Conclusion

The static method in Python is much easier to understand once you focus on what the method actually needs. An instance method receives self because it works with a particular object. A class method receives cls because it works with the class. A static method receives neither automatically because it can perform its job without either one.

The @staticmethod decorator is therefore less about adding complexity and more about communicating intent. If you have a function that logically belongs to a class but doesn’t need instance or class data, a static method can provide a clean and organized solution. Python’s official documentation confirms that static methods can be called from both classes and instances without receiving an implicit first argument.

Once you understand the difference between self, cls, and no automatic argument, choosing between instance methods, class methods, and static methods becomes much more straightforward. The goal isn’t to use @staticmethod everywhere; it’s to use it when it makes the design of your Python code clearer.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top