Static methods in Python are incredibly similar to Python class-level methods, the difference being that a static method is bound to a class rather than the objects for that class.
Python staticmethod()
Python staticmethod() is a built-in function that returns a static method for a given function. This method is similar to the Python class-level method, but the difference is, this method is only bound to class rather than class objects. Therefore, we cannot modify any class object using the staticmethod() function as objects are not bound to it.
This means that a static method can be called without an object for that class. This also means that static methods cannot modify the state of an object as they are not bound to it.
Let’s see how we can create static methods in Python.
Syntax
staticmethod(function)
Arguments
The function argument is the function present inside the class and needs to be converted.
However, the above method is an un-Pythonic way to create a staticmethod; the newer way is:
@staticmethod def func(args, ...)
Here, we have used @staticmethod.
The static method returns a static method of a given function.
Creating python static methods
Python Static methods can be created in two ways. Let’s see the first method.
Using staticmethod()
Let’s see how to use the staticmethod() approach.
# app.py class Mul: def calculate(x, y): return x * y # create addNumbers static method Mul.calculate = staticmethod(Mul.calculate) print('Product:', Mul.calculate(11, 21))
Output
➜ pyt python3 app.py Product: 231 ➜ pyt
One thing to note is that we called the mul method we created without an object.
There were no surprises there. This approach is controlled at each place, and it is possible to create a static method out of a class method. Let’s see another approach with the same example here.
See another example.
# app.py # Defining a class named Sum class Sum: # defining a function sumoftwo # this function will be called in a static method def sumoftwo(a, b): return a+b # Creating Static Method Sum.sumoftwo = staticmethod(Sum.sumoftwo) # Printing values by passing arguments print("Sum of two numbers are: ", Sum.sumoftwo(10, 20))
Output
➜ pyt python3 app.py Sum of two numbers is: 30 ➜ pyt
In this example, we have first created a class named ‘Sum’ then created a function inside the class called ‘sumoftwo’.
We have created a static method outside the class by calling this function, and then we have printed that.
Using @staticmethod
This is a more subtle way of creating a Static method as we do not have to rely on a statement definition of a method being a class method and making it static at each place you make it static.
See the following example.
# app.py # Defining a class named Sum class Sum: # defining a function sumoftwo # this function will be called in a static method # Creating static method here only @staticmethod def sumoftwo(a, b): return a+b # Printing values by passing agruments print("Sum of two numbers are: ", Sum.sumoftwo(10, 20))
Output
➜ pyt python3 app.py Sum of two numbers are: 30 ➜ pyt
In this example, like example 1, we have created a class, but when we create the function inside the class, we declare it as a static method, and then outside the class, we pass values and print the desired output.
Advantages of Python static method
Static methods have an obvious use case. When we need some functionality, not concerning an Object, but concerning the complete class, we make a method static.
This is advantageous when we need to create Utility methods as they aren’t tied to an object lifecycle.
Finally, note that we don’t need the self to be passed as the first argument in a static method.
That’s it for this tutorial.