In object-oriented programming, besides instance methods, we can define class methods and static methods in a class.
- Class methods: Used to access or modify the state of the class. If a method uses only class variables, declare it as a class method.
- Static methods: A static method is a general utility method that performs a task in isolation. It doesn’t use instance or class variables, because it doesn’t receive
selforcls.
Also, read Python Class method vs Static method vs Instance method.
After reading this article, you’ll learn:
- How to create and use static methods in Python
- How to create a static method using the
@staticmethoddecorator and thestaticmethod()function - When to use a static method, and when not to
- How to call a static method from other methods
Table of contents
What is Static Methods in Python
A static method is a general utility method that performs a task in isolation. Static methods in Python are similar to static methods in Java or C++.
A static method belongs to the class namespace, but it isn’t bound to the class or to an object. It’s a plain function placed inside the class because it’s logically related to it. We usually call it using the class name.
A static method doesn’t have access to class or instance variables, because it doesn’t receive an implicit first argument like self or cls. Therefore, it can’t modify the state of the object or the class.
A static method can be called using ClassName.method_name() or through an object of the class.
Output:
Inside static method 10 Inside static method 10
Define Static Method in Python
Every method you define in a class is an instance method by default. To make it a static method, you must tell Python explicitly using the @staticmethod decorator or the staticmethod() function.
Static methods are defined inside a class, much like a regular function. To declare a static method, use this idiom:
class C:
@staticmethod
def f(arg1, arg2, ...): ...Code language: Python (python)
Example: Create Static Method Using @staticmethod Decorator
To make a method a static method, add the @staticmethod decorator before the method definition. Don’t add self or cls as a parameter.
@staticmethod is a built-in decorator. Python applies it right after the function is defined and turns the function into a static method.
Sometimes you’ll write code that belongs to a class but doesn’t use the object itself at all. Such a utility method doesn’t need an object (the self parameter) to do its job, so we declare it as a static method. Other methods of the class can still call it.
In this example, we create a static method gather_requirement() that accepts a project name and returns all the tasks required to complete that project.
Output:
Completed task_1 Completed task_2 Completed task_3
Advantages of a Static Method
A static method has the following advantages:
- Readability: When you see
@staticmethodabove a method, you immediately know that the method doesn’t depend on the object’s state or the class state. - Utility functions with a clear home: Static methods can’t access an object’s attributes (instance variables) or class attributes (class variables). This makes them a good fit for helpers such as validation or converting a value from one unit to another, where the parameters provide everything the method needs. Keeping them inside the class groups them with related code.
- No binding needed: Python doesn’t need to bind a static method to an object. When you access it through an object or the class, you get the same plain function every time.
Note: A common myth is that every object gets its own copy of each instance method, and that static methods save memory. That’s not true. All methods, of every kind, are stored only once, in the class. When you access an instance method through an object, Python creates a small temporary bound method that links the function to that object. That’s why kelly.work is jessa.work is False, even though both use the same function. The following code shows this.
When to Use a Static Method
Use a static method when the method:
- doesn’t need to read or change instance variables (no
self), and - doesn’t need to read or change class variables or create objects of the class (no
cls), but - is closely related to the class, so it makes sense to keep it there.
If the method needs the class (for example, to create a new object or read a class variable), use a class method instead. If it isn’t related to the class at all, a regular module-level function is often simpler.
Example: A temperature class with static helper methods for conversion and validation.
Output:
212.0 25 C = 77.0 F False
The staticmethod() function
Some older code defines static methods by calling staticmethod() as a function rather than using it as a decorator.
The decorator syntax has been available since Python 2.4, so in modern code you should always use the @staticmethod decorator. The staticmethod() function is mainly useful when you add a static method to a class at runtime.
Syntax:
staticmethod(function)Code language: Python (python)
function: The function you want to convert into a static method.- It returns the converted static method.
Example:
Output:
Inside static method 10
The staticmethod() approach is helpful when you need to store a reference to a function in a class and you want to stop Python from automatically turning it into an instance method.
Call Static Method from Another Method
You can call a static method from any other method of the same class:
- From an instance method:
self.static_method()(as in thework()example above) - From a class method:
cls.static_method() - From another static method:
ClassName.static_method(), because a static method has noselforcls
In the following example, a class method calls a static method, which in turn calls another static method.
Output:
static method 1
Summary
- A static method is a utility function defined inside a class. It takes neither
selfnorcls. - Create it with the
@staticmethoddecorator (preferred) or thestaticmethod()function. - It can’t access or modify instance variables or class variables.
- Call it with
ClassName.method()or through an object. - Use it for helpers that are related to the class but don’t need its data. Use a class method if you need the class.
- Static methods don’t save memory compared to instance methods. Every method is stored once in the class.
Reference: Static method documentation
