Python Static Method

Learn what a static method is in Python and how to use @staticmethod. See examples, static method vs class method vs instance method, when to use static methods, and common mistakes.

Published

Updated

Read time 6 min read

Reviewed byDeepak Prasad

Python Static Method

A static method is a method defined inside a class using @staticmethod. It behaves like a regular function, but it lives in the class namespace because it is related to that class.

Tested on: Python 3.13.3; kernel 6.14.0-37-generic.


Quick answer: Python static method

A static method does not receive self or cls automatically. Use it when the method does not need instance data or class data.

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

print(Calculator.add(5, 3))
Output

No object is required. You call the method on the class with the arguments it needs.


Python static method quick reference

Task Use
Define static method @staticmethod
Call using class ClassName.method(args)
Call using instance obj.method(args)
Instance access No automatic self
Class access No automatic cls
Best use Utility or helper related to the class
Use instance method instead When method needs self
Use class method instead When method needs cls
Use module function instead When helper is not strongly related to a class

What is a static method in Python?

  • A static method is a method inside a class that does not receive self or cls.
  • It behaves like a normal function placed in a class namespace.
  • You can call it using the class name.
  • You can also call it using an instance, but class-name access is usually clearer.
  • It cannot directly access instance or class state unless you pass that data as an argument.

Static methods are useful when the logic belongs near a Python class but does not depend on a particular object or on class-level state.


Python @staticmethod syntax

Use @staticmethod above the method definition. Do not include self or cls as the first parameter. Define only the parameters the method actually needs.

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

    @staticmethod
    def multiply(a, b):
        return a * b
Output

Basic Python static method example

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

result = Calculator.add(5, 3)
print(result)
Output

The method performs a simple calculation. It does not read or change object state, so no instance is required.


Call a static method from class and object

Both calling styles work. The instance is not passed as self.

python
class Greeter:
    @staticmethod
    def greet(name):
        return f"Hello, {name}"

print(Greeter.greet("Alex"))

obj = Greeter()
print(obj.greet("Sam"))
Output

Prefer ClassName.method() when the method does not depend on the object. That makes the intent clearer to readers.


Static method vs instance method vs class method

Method type Decorator First argument Can access instance state? Can access class state? Best for
Instance method None self Yes Yes, indirectly Behavior of one object
Class method @classmethod cls No Yes Alternative constructors, class-level logic
Static method @staticmethod None No No Utility or helper related to the class

Instance methods work with object state. Class methods work with class-level data through cls. Static methods are utilities that do not need either.


Static method vs class method

Use @staticmethod when the method does not need class data. Use @classmethod when the method needs the class through cls.

For alternative constructors, @classmethod is usually the better choice because cls lets subclasses reuse the constructor correctly:

python
class Dog:
    def __init__(self, name, breed):
        self.name = name
        self.breed = breed

    @classmethod
    def from_csv(cls, text):
        name, breed = text.split(",")
        return cls(name.strip(), breed.strip())

class Puppy(Dog):
    pass

dog = Puppy.from_csv("Max, Beagle")
print(dog.name, type(dog).__name__)
Output

A static factory that hard-codes Dog(...) would not create a Puppy instance when called on the subclass. That is why factory methods usually belong on @classmethod, not @staticmethod—and why they still delegate to the normal __init__ path through cls(...).


Static method vs normal function

A static method is basically a normal Python function stored inside a class namespace.

  • Use a static method when the helper is strongly related to the class.
  • Use a module-level function when the helper is general-purpose.
  • Do not put every helper inside a class just because it is possible.

If the function has no natural connection to the class, keep it at module scope.

python
def clamp(value, low, high):
    return max(low, min(value, high))


class Email:
    @staticmethod
    def is_valid(address):
        return "@" in address and "." in address.split("@")[-1]


print(clamp(150, 0, 100))
print(Email.is_valid("user@example.com"))
print(Email.is_valid("not-an-email"))
Output

clamp() is a general utility, so it lives at module scope. is_valid() belongs with Email because it validates data for that class, even though it does not need self or cls.


When should you use static methods?

Use static methods when:

  • The function is logically related to a class.
  • It does not need self.
  • It does not need cls.
  • It is a small helper used by class methods or instance methods.
  • You want validation or conversion logic near the class.

Good examples include validate_email(), normalize_name(), calculate_area(radius), is_valid_username(username), and parse_simple_value(text).


When should you not use static methods?

Avoid static methods when:

  • The method needs instance attributes.
  • The method needs class attributes or subclass behavior.
  • The method is a general helper unrelated to the class.
  • You are only using a class as a namespace for random functions.
  • A normal module-level function would be simpler.

Static method for validation

Validation helpers are a common good use:

python
class User:
    @staticmethod
    def is_valid_username(username):
        return len(username) >= 5 and username.isalnum()

    @staticmethod
    def is_valid_age(age):
        return isinstance(age, int) and 0 <= age <= 120

print(User.is_valid_username("john"))
print(User.is_valid_username("johnny"))
print(User.is_valid_age(25))
Output

You can validate input before creating a User object without building an instance first.


Static method for conversion or formatting

Static methods can format or transform values related to the class:

python
class Product:
    @staticmethod
    def normalize_name(name):
        return " ".join(name.strip().split()).title()

    @staticmethod
    def format_price(amount):
        return f"${amount:.2f}"

print(Product.normalize_name("  wireless   mouse  "))
print(Product.format_price(19.5))
Output

If conversion creates a new instance of the class, consider @classmethod instead so subclasses stay correct.


Can static methods access class variables?

Static methods do not receive cls automatically. They can reference a class variable only if you use the class name directly:

python
class Circle:
    PI = 3.141592653589793

    @staticmethod
    def area(radius):
        return Circle.PI * radius * radius

print(Circle.area(5))
Output

If the method really depends on class state or subclass behavior, use @classmethod instead of hard-coding the class name.


Can static methods be inherited or overridden?

Static methods are class attributes, so subclasses inherit them. A subclass can define another method with the same name:

python
class Base:
    @staticmethod
    def helper():
        return "base"

class Sub(Base):
    @staticmethod
    def helper():
        return "sub"

print(Base.helper())
print(Sub.helper())
Output

Static methods do not receive cls, so they do not automatically adapt to subclass state the way class methods do. Inheritance and overriding are possible; automatic subclass awareness is not.


@staticmethod decorator vs staticmethod() function

Most code uses decorator syntax:

python
class Example:
    @staticmethod
    def show():
        return "decorator form"
Output

You can also assign the built-in wrapper directly:

python
class Example:
    def show():
        return "function form"

    show = staticmethod(show)
Output

Prefer @staticmethod in normal application code. It is clearer and easier to maintain.


Summary

Use @staticmethod to define a method that does not receive self or cls. Static methods are useful for class-related helper functions such as validation and formatting. Use instance methods when you need object state. Use class methods when you need class state or subclass-aware constructors. Use module-level functions for general utilities that are not closely tied to a class.


References


Frequently Asked Questions

1. What is a static method in Python?

A static method is a method defined inside a class with @staticmethod. It does not receive self or cls automatically and behaves like a regular function grouped with the class.

2. What is the difference between static method and class method?

A static method does not receive cls. A class method receives cls and can access class state or build subclass-aware constructors.

3. Can a static method access instance attributes?

No. Static methods do not receive self, so they cannot access instance attributes unless you pass that data as an argument.

4. Can static methods be inherited or overridden?

Yes. Static methods are class attributes, so subclasses inherit them and can define another method with the same name.

5. When should you use @staticmethod instead of a normal function?

Use @staticmethod when the helper is logically related to the class but does not need self or cls. Use a module-level function for general utilities.

6. Should factory methods use @staticmethod?

Usually no. Alternative constructors that create instances often work better as @classmethod because cls supports subclass-aware construction.
Bashir Alam

Data Analyst and Machine Learning Engineer

Computer Science graduate from the University of Central Asia, currently employed as a full-time Machine Learning Engineer at uExel. His expertise lies in OCR, text extraction, data preprocessing, and …