In Object-oriented programming, at the class level, we use class methods and static methods.
- Class methods: Used to access or modify the state of the class. if we use only class variables, we should declare such methods as a class method.
- Static methods: A static method is a general utility method that performs a task in isolation. Inside this method, we don’t use instance or class variable because this static method doesn’t take any parameters like
After reading this article, you’ll learn:
- How to create and use the static methods in Python
- Create staticmethod using the
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 those found in Java or C++.
A static method is bound to the class and not the object of the class. Therefore, we can call it using the class name.
A static method doesn’t have access to the class and instance variables because it does not receive an implicit first argument like
cls. Therefore it cannot modify the state of the object or class.
The class method can be called using
ClassName.method_name() as well as by using an object of the class.
class Employee: @staticmethod def sample(x): print('Inside static method', x) # call static method Employee.sample(10) # can be called using object emp = Employee() emp.sample(10)
Define Static Method in Python
Any method we create in a class will automatically be created as an instance method. We must explicitly tell Python that it is a static method using the
@staticmethod decorator or
Static methods are defined inside a class, and it is pretty similar to defining a regular function. To declare a static method, use this idiom:
class C: @staticmethod def f(arg1, arg2, ...): ...
Example: Create Static Method Using
To make a method a static method, add
@staticmethod decorator before the method definition.
@staticmethod decorator is a built-in function decorator in Python to declare a method as a static method. It is an expression that gets evaluated after our function is defined.
In this example, we will create a static method
gather_requirement() that accepts the project name and returns all requirements to complete under this project.
Static methods are a special case of methods. Sometimes, you’ll write code that belongs to a class, but that doesn’t use the object itself at all. It is a utility method and doesn’t need an object (
self parameter) to complete its operation. So we declare it as a static method. Also, we can call it from another method of a class.
class Employee(object): def __init__(self, name, salary, project_name): self.name = name self.salary = salary self.project_name = project_name @staticmethod def gather_requirement(project_name): requirement = ['task_1', 'task_2', 'task_3'] return requirement # instance method def work(self): # call static method from instance method requirement = self.gather_requirement(self.project_name) for task in requirement: print('Completed', task) emp = Employee('Kelly', 12000, 'ABC Project') emp.work()
Completed task_1 Completed task_2 Completed task_3
Advantages of a Static Method
Here, the static method has the following advantages
- Consume Less memory: Instance methods are object too, and creating them has a cost. Having a static method avoids that. Let’s assume you have ten employee objects and if you create
gather_requirement()as a instance method then Python have to create a ten copies of this method (seperate for each object) which will consume more memeory. On the other hand static method has only one copy per class.
kelly = Employee('Kelly', 12000, 'ABC Project') jessa = Employee('Jessa', 7000, 'XYZ Project') # false # because seperate copy of instance method is created for each object print(kelly.work is jessa.work) # True # because only one copy is created # kelly and jess objects share the same methods print(kelly.gather_requirement is jessa.gather_requirement) # True print(kelly.gather_requirement is Employee.gather_requirement)
- To Write Utility functions: Static methods have limited use because they don’t have access to the attributes of an object (instance variables) and class attributes (class variables). However, they can be helpful in utility such as conversion form one type to another. The parameters provided are enough to operate.
- Readabiltity: Seeing the
@staticmethodat the top of the method, we know that the method does not depend on the object’s state or the class state.
Some code might use the old method of defining a static method, using
staticmethod() as a function rather than a decorator.
You should only use
staticmethod() function to define static method if you have to support older versions of Python (2.2 and 2.3). Otherwise, it is recommended to use the
function: It is the name of the method you want to convert as a static method.
- It returns the converted static method.
class Employee: def sample(x): print('Inside static method', x) # convert to static method Employee.sample = staticmethod(Employee.sample) # call static method Employee.sample(10)
staticmethod() approach is helpful when you need a reference to a function from a class body and you want to avoid the automatic transformation to the instance method.
Call Static Method from Another Method
Let’s see how to call a static method from another static method of the same class. Here we will class a static method from a class method.
class Test : @staticmethod def static_method_1(): print('static method 1') @staticmethod def static_method_2() : Test.static_method_1() @classmethod def class_method_1(cls) : cls.static_method_2() # call class method Test.class_method_1()
static method 1
Referance: Static method documentation