Static methods in Python are extremely 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.
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.
In this quick post, we will learn how to create and use a Python static method. We will also have a look at what advantages and disadvantages static methods offer as compared to the instance methods.
The function argument is the function that is present inside the class, and that needs to be converted.
However, the above method is a 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.
Python Static methods can be created in two ways. Let’s see each of the ways here:
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))
➜ pyt python3 app.py Product: 231 ➜ pyt
One thing to note 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 as well. 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))
➜ pyt python3 app.py Sum of two numbers is: 30 ➜ pyt
In this example, we have first created a class named ‘Sum’ then we have created a function inside the class named ‘sumoftwo’.
Outside the class, we have created a static method by calling this function, and then we have printed that.
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))
➜ 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 are creating the function inside the class, we are declaring it as a static method, and then outside the class, we are passing values and printing the desired output.
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 pretty much advantageous when we need to create Utility methods as they aren’t tied to an object lifecycle usually.
Finally, note that in a static method, we don’t need the self to be passed as the first argument.
Finally, Python Static Method | staticmethod() and @staticmethod Example are over.
Thanks for reading !
In the programming world, Data types play an important role. Each Variable is stored in different data types and responsible for various functions. Python had two different objects, and They are mutable and immutable objects.
Are you looking for experienced, reliable, and qualified Python developers? If yes, you have reached the right place. At **[HourlyDeveloper.io](https://hourlydeveloper.io/ "HourlyDeveloper.io")**, our full-stack Python development services...
After analyzing clients and market requirements, TopDevelopers has come up with the list of the best Python service providers. These top-rated Python developers are widely appreciated for their professionalism in handling diverse projects. When...
Python Hello World Program - Your first step towards Python world. Learn how to create the Hello World Python program in PyCharm.
This video on 'Standard Data Types in Python' will help you establish a foothold on Python by helping you learn basic concepts.