Question
In Python, can I define a static method that can be called directly on the class, like this?
MyClass.the_static_method()
If so, how do static methods work, and how are they different from regular instance methods?
Short Answer
By the end of this page, you will understand what a static method is in Python, how to define one with @staticmethod, and how to call it on a class or an instance. You will also see how static methods differ from instance methods and class methods, and when they are a good design choice.
Concept
In Python, a static method is a method defined inside a class that does not automatically receive either:
- the instance (
self) - the class (
cls)
That means it behaves much like a regular function, but it is placed inside the class because it is logically related to that class.
You define a static method using the @staticmethod decorator:
class MyClass:
@staticmethod
def the_static_method():
print("Hello from a static method")
Then you can call it directly on the class:
MyClass.the_static_method()
You can also call it on an instance:
obj = MyClass()
obj.the_static_method()
Why this matters
In real programs, not every function inside a class needs access to object state. Sometimes you want:
- a helper function related to the class
- validation logic
- formatting or conversion logic
- utility behavior grouped with the class for organization
Static methods help you keep related code together without pretending the method needs self or cls.
The key idea
If a method needs:
- instance data → use an instance method
- class-level data → use a class method
- neither → use a static method
Mental Model
Think of a class as a toolbox.
- An instance method is a tool that needs a specific toolbox item to work on, so it receives
self. - A class method is a tool that needs to know which toolbox it belongs to, so it receives
cls. - A static method is just a useful tool stored in the toolbox drawer. It does not need the toolbox or any item from it to work.
So a static method is like a calculator kept inside a "MathTools" box. The calculator belongs there logically, but it does not need the box itself to do addition.
Syntax and Examples
The basic syntax is:
class MyClass:
@staticmethod
def greet(name):
return f"Hello, {name}!"
Call it on the class:
print(MyClass.greet("Ava"))
Output:
Hello, Ava!
You can also call it on an instance:
obj = MyClass()
print(obj.greet("Liam"))
Example: grouping utility logic inside a class
class Temperature:
@staticmethod
def celsius_to_fahrenheit(c):
return (c * 9 / 5) + 32
print(Temperature.celsius_to_fahrenheit(25))
Output:
Step by Step Execution
Consider this example:
class MathHelper:
@staticmethod
def add(a, b):
return a + b
result = MathHelper.add(3, 4)
print(result)
Step by step:
- Python reads the
MathHelperclass definition. - It sees the
@staticmethoddecorator aboveadd. - Python stores
addas a static method on the class. MathHelper.add(3, 4)is called.- No
selforclsis passed automatically. - The values
3and4are passed directly intoaandb. - The method returns
7. print(result)outputs:
Real World Use Cases
Static methods are useful when behavior belongs with a class conceptually, but does not need instance or class state.
Common examples
- Validation helpers
- checking whether an email format is valid
- checking whether a password meets rules
- Conversion utilities
- Celsius to Fahrenheit
- kilometers to miles
- string to normalized slug
- Parsing helpers
- splitting a log line
- cleaning user input
- Domain-specific utility functions
- tax calculation rules
- scoring formulas
- date formatting helpers
Example: validation
class UserValidator:
@staticmethod
def is_valid_username(username):
return len(username) >= 3 and username.isalnum()
print(UserValidator.is_valid_username("sam123"))
Example: formatting
class ReportFormatter:
@staticmethod
def title_case():
text.title()
(ReportFormatter.title_case())
Real Codebase Usage
In real projects, developers often use static methods in small, focused ways.
Common patterns
Helper methods inside a class
A class may contain small utility functions that support the main behavior.
class PriceCalculator:
@staticmethod
def apply_tax(amount, rate):
return amount * (1 + rate)
Validation before object creation
class User:
def __init__(self, username):
if not self.is_valid_username(username):
raise ValueError("Invalid username")
self.username = username
@staticmethod
def is_valid_username(username):
return len(username) >= 3
This keeps validation logic close to the class.
Parsing or normalization
Common Mistakes
1. Forgetting the @staticmethod decorator
Broken example:
class MyClass:
def hello(name):
return f"Hello, {name}"
obj = MyClass()
print(obj.hello("Ava"))
Why it fails:
objis automatically passed as the first argument- Python tries to use
objasname - the arguments no longer match what you intended
Fix:
class MyClass:
@staticmethod
def hello(name):
return f"Hello, {name}"
2. Using a static method when you actually need self
Broken example:
:
():
.value =
():
.value +=
Comparisons
| Method type | First parameter | Can access instance data? | Can access class data? | Typical use |
|---|---|---|---|---|
| Instance method | self | Yes | Yes, through class/instance | Behavior tied to one object |
| Class method | cls | Not directly | Yes | Alternative constructors, class-wide behavior |
| Static method | None automatic | No | No | Utility logic related to the class |
Example comparison
class Example:
class_value = 10
def instance_method():
.class_value
():
cls.class_value
():
x + y
Cheat Sheet
class MyClass:
@staticmethod
def my_method(arg1, arg2):
return arg1 + arg2
Call forms:
MyClass.my_method(1, 2)
obj = MyClass()
obj.my_method(1, 2)
Rules
- Use
@staticmethodfor methods that need neitherselfnorcls - Static methods can be called on:
- the class
- an instance
- Static methods do not get automatic first arguments
- If you need instance state, use an instance method
- If you need class state, use a class method
Quick decision guide
- Needs object data? →
def method(self): - Needs class data? →
@classmethod - Needs neither? →
@staticmethod
Related syntax
FAQ
Can I call a static method directly on a Python class?
Yes. That is one of its main uses.
MyClass.the_static_method()
Can I call a static method on an instance too?
Yes. Python allows both:
obj = MyClass()
obj.the_static_method()
Does a static method receive self automatically?
No. A static method receives no automatic first argument.
When should I use @staticmethod in Python?
Use it when the method belongs logically to the class but does not need instance data or class data.
What is the difference between @staticmethod and @classmethod?
@staticmethodgets no automatic argument@classmethodreceivescls, the class itself
Should I use a static method or a normal function?
If the logic is closely related to a class, a static method can improve organization. If it is general-purpose, a regular function is often better.
Can a static method access class variables?
Not automatically. It can still refer to the class by name explicitly, but if the behavior depends on the class itself, is usually the better design.
Mini Project
Description
Build a small utility class for text processing. This project demonstrates how static methods can group related helper functions inside a class without needing object state. It mirrors real code where formatting and validation helpers are kept close to the domain they support.
Goal
Create a TextTools class with static methods that clean, format, and validate strings.
Requirements
- Create a class named
TextTools - Add a static method that trims whitespace from a string
- Add a static method that converts text to lowercase
- Add a static method that checks whether a string is empty after trimming
- Call the static methods directly on the class and print the results
Keep learning
Related questions
@staticmethod vs @classmethod in Python Explained
Learn the difference between @staticmethod and @classmethod in Python with clear examples, use cases, mistakes, and a mini project.
Call a Function by Name in a Python Module
Learn how to call a function by name in a Python module using strings, getattr, and safe patterns for dynamic function dispatch.
Catch Multiple Exceptions in One except Block in Python
Learn how to catch multiple exceptions in one Python except block using tuples, with examples, mistakes, and real-world usage.