AppDividend
Latest Code Tutorials

Python Static Method | staticmethod() and @staticmethod

0

Python static method 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. It means we cannot modify any class object using the staticmethod() function as objects are not bound to it.

Python Static Method

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.

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)

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.

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 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))

Output

➜  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.

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 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.

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 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.

Related Posts

Python super() function

Python classmethod() function

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.