Python Static Methods Tutorial

In this section, we will learn about static methods in Python.

Note: we’re assuming you’re already familiar with the Python instance methods.

What is Static Methods in Python?

When creating instance methods in a class, we know that every time an object is created from a class, it will take its own independent instance method! That means if there are 4 objects from a class, then there will be 4 instance methods as well.

But sometimes we only need one method in order to run a set of tasks that does not depend on any class or objects! For example, a method might be needed to run only a multiplication operation! For such a method, there’s no need to create an object first and then invoke the method! That would be a waste of memory!

So for this reason, python provided a way of creating methods in classes that can be executed without the need to create an object first!

These methods are called static methods.

These methods can be directly invoked using the name of the owner class.

Note that using static methods, we can’t access the members of a class! A static method is completely independent from any member of that class and so unless we create an instance in the body of a static method first and then use that instance object, there’s no way of accessing the members of a class using the static method.

Static Method in Python: Syntax and Declaration

class ClassName: 
    @staticmethod
    def functionName():
        #body…

The way we create a static method is exactly the same as the process of creating other functions in Python, with the exception that we annotate a static method with `@staticmethod`.

Basically put the `@staticmethod` annotation on top of a method and that becomes a static method.

Note: static methods do not take `self` or `clr` parameter as their first parameter! This is because a static method is not bound to neither class or an object of that class.

Example: creating static methods in Python

class Parent: 

    def __init__ (self, firstName, lastName):
        self.firstName = firstName
        self.lastName = lastName
        
    def instanceMethod(self):
        print(f"This is an instance method!")

    @staticmethod
    def statmethod2():
        print("Hello")

    @staticmethod
    def statMethod(param1, param2):
        return param1 * param2

The Parent class in this example has two static methods, `statMethod2` and `statMethod`.

The `statmethod2` does not take any argument and if called, it will simply print the message `Hello` and the other static method `statMethod` takes two arguments, multiply them and finally returns the result.

Accessing Static Methods in Python

In order to access a static method of a class, we use the name of that class + the dot `.` operator.

Example: accessing static methods in python

class Parent: 

    def __init__ (self, firstName, lastName):
        self.firstName = firstName
        self.lastName = lastName
        
    def instanceMethod(self):
        print(f"This is an instance method!")

    @staticmethod
    def statmethod2():
        print("Hello")

    @staticmethod
    def statMethod(param1, param2):
        return param1 * param2


print(Parent.statMethod(40, 60))

Parent.statmethod2()

Output:

2400

Hello

As you can see, in order to call a static method, we only need to use the name of the parent class! There’s no need for object creation.

Facebook
Twitter
Pinterest
LinkedIn

Top Technologies