staticmethod()
The built-in staticmethod() function is a decorator that lets you transform a method into a static method. Static methods don’t receive an implicit first argument, meaning they don’t have access to the instance (self) or class (cls) objects. Instead, they behave like regular functions defined within a class:
>>> class DemoClass:
... @staticmethod
... def static_method():
... return "Hello from static method!"
>>> DemoClass.static_method()
'Hello from static method!'
staticmethod() Syntax
@staticmethod
def method(*args, **kwargs):
...
staticmethod(method)
Arguments
| Argument | Description |
|---|---|
method |
The method to be transformed into a static method |
Return Value
- Returns a static method object that can be called on a class or its instances.
staticmethod() Examples
With a function to be transformed into a static method:
>>> class Greeter:
... @staticmethod
... def greet(name):
... return f"Hello, {name}!"
...
>>> Greeter.greet("Pythonista")
'Hello, Pythonista!'
With an instance of the class:
>>> instance = Greeter()
>>> instance.greet("Developer")
'Hello, Developer!'
Calling staticmethod() directly instead of using it as a decorator keeps a reference to an existing function in a class body without the automatic transformation to an instance method:
>>> def shout(text):
... return f"{text.upper()}!"
...
>>> class Messenger:
... announce = staticmethod(shout)
...
>>> Messenger.announce("hello")
'HELLO!'
staticmethod() Common Use Cases
The most common use cases for the staticmethod() function include:
- Grouping utility functions within a class.
- Providing a function that doesn’t need access to class or instance data.
- Creating a namespace for related functions.
staticmethod() Real-World Example
Consider a Formatter class that provides methods to format numbers as currency and percentages. These methods don’t rely on instance or class data, so they can be defined as static methods:
formatter.py
class Formatter:
@staticmethod
def as_currency(value):
return f"${value:,.2f}"
@staticmethod
def as_percent(value):
return f"{value:.2%}"
# Usage
formatter = Formatter()
print(formatter.as_currency(1000)) # Output: $1,000.00
print(formatter.as_percent(0.25)) # Output: 25.00%
In this example, .as_currency() and .as_percent() are static methods that format numbers without needing access to any instance or class variables. This helps to logically group utility functions within the Formatter class.
Related Resources
Course
OOP Method Types in Python: @classmethod vs @staticmethod vs Instance Methods
What's the difference between @classmethod, @staticmethod, and "plain/regular" instance methods in Python? You'll know the answer after watching this video course.
For additional information on related topics, take a look at the following resources:
- Python's Instance, Class, and Static Methods Demystified (Tutorial)
- Python Classes: The Power of Object-Oriented Programming (Tutorial)
- Object-Oriented Programming (OOP) in Python (Tutorial)
- Providing Multiple Constructors in Your Python Classes (Tutorial)
- Python's Instance, Class, and Static Methods Demystified (Quiz)
- Class Concepts: Object-Oriented Programming in Python (Course)
- Inheritance and Internals: Object-Oriented Programming in Python (Course)
- Python Classes - The Power of Object-Oriented Programming (Quiz)
- A Conceptual Primer on OOP in Python (Course)
- Intro to Object-Oriented Programming (OOP) in Python (Course)
- Object-Oriented Programming (OOP) in Python (Quiz)
- Using Multiple Constructors in Your Python Classes (Course)