Python Static Method

The Python static Method can call without creating an instance or object. Although Static Method belongs to a class, you can call directly by its name. If you want a method that doesn’t need any class variables or instance variables to operate, then you can create it as a static method.

For example, if you want to print a welcome message, then you can create it as a static method because it won’t change. Or it won’t need any class variable to display that message. We can create a static method in Python by using @staticmethod decorator or staticmethod() function. Although we don’t recommend the second option, we are showing the possibilities for your understanding.

In this section, we explain to you how to create a static method in the Python programming language and how to call the static methods. In most of the examples, we combine static methods with the non-static methods. At first, it might confuse you, but this allows you to differentiate between static and non-static methods.

Python static Method Example

You can write both static and non-static methods with a single class. Let me create a non-static method func_message() and one static method func_msg() using @staticmethod decorator.

TIP: Refer to the Python Functions and class article to create a Python class.

class Employee:
     
    def func_message(self):
        print('Welcome to Python Programming')
 
    @staticmethod
    def func_msg():
        print("Welcome to Tutorial Gateway")

To display something from a class, you need to create an object from that class.

Calling static Method

In this example, we show how to call Python static method and non-static method. As we said earlier, we don’t need to create an instance to call the static methods.

If you look into the example, Employee. func_msg() means classname. staicmethod_name. It automatically calls the func_msg method in the employee class. Next, we created an instance of the Employee class to call another method, which is a non-static method.

class Employee:
     
    def func_message(self):
        print('Welcome to Python Programming')
 

    @staticmethod
    def func_msg():
        print("Welcome to Tutorial Gateway")
 
Employee.func_msg()
 
emp = Employee()
emp.func_message()

Calling Python static method

Welcome to Tutorial Gateway
Welcome to Python Programming

Static Method approach 2

In this example, we create a static method using staticmethod() function in Python. Within the class, first, we created two regular methods. Next, we used the below statement to convert the regular method to a static method.

Employee.func_addition = staticmethod (Employee. func_addition). It means class_name. method_name = staticmethod (class_name. method_name)

class Employee:
     
    def func_message(self):
        print('Welcome to Python Programming')
 
    def func_addition(a, b):
        return a + b
     
Employee.func_addition = staticmethod(Employee.func_addition)
print('Total = ', Employee.func_addition(25, 50))
 
emp = Employee()
emp.func_message()

Creating a static method output

Total =  75
Welcome to Python Programming

It is another example of creating a static method using the staticmethod() function. Here, we created two static methods func_addition() and func_multiply()

class Employee:
     
    def func_message(self):
        print('Welcome to Python Programming')
 
    def func_addition(a, b):
        return a + b
 
    def func_multiply(a, b):
        return a * b
 
emp = Employee()
emp.func_message()
 
Employee.func_addition = staticmethod(Employee.func_addition)
print('Total = ', Employee.func_addition(25, 50))
 
Employee.func_multiply = staticmethod(Employee.func_multiply)
print('Multiplication = ', Employee.func_multiply(25, 50))

static method output

Welcome to Python Programming
Total =  75
Multiplication =  1250

Python Static Method with Arguments example

In Python, you don’t have to use self or cls attributes to create a static method. Like a function, you can create it and add the arguments as per your requirements.

In this example, we created two static methods. The def func_msg() method prints a welcome message. And, def split_string (message) method accepts a string and splits that string by comma delimiter. I suggest you refer to String split article.

class Employee:
     
    def func_message(self):
        print('Welcome to Python Programming')
 
    @staticmethod
    def func_msg():
        print("Welcome to Tutorial Gateway")
 
    @staticmethod
    def split_string(message):
        return message.split(",")
 
Employee.func_msg()
 
countries = 'India, China, Japan, USA, UK, Australia, Canada'
 
print(Employee.split_string(countries))

Python @staticmethod with arguments output

Welcome to Tutorial Gateway
['India', ' China', ' Japan', ' USA', ' UK', ' Australia', ' Canada']

It is another example of a Python static method with arguments. In this example, we used static methods to split the message using comma and used the replace function to replace the comma with an underscore. I suggest you refer String replace article.

class Employee:
     
    def func_message(self):
        print('Welcome to Python Programming')
 
    @staticmethod
    def func_msg():
        print("Welcome to Tutorial Gateway")
 
    @staticmethod
    def split_string(message):
        return message.split(",")
 
    @staticmethod
    def replace_string(message):
        return message.replace(",", "_")
 
Employee.func_msg()
 
countries = 'India, China, Japan, USA, UK, Australia, Canada'
 
print(Employee.split_string(countries))
print(Employee.replace_string(countries))

Python @staticmethod with arguments output

Welcome to Tutorial Gateway
['India', ' China', ' Japan', ' USA', ' UK', ' Australia', ' Canada']
India_ China_ Japan_ USA_ UK_ Australia_ Canada

Arithmetic Operations using Python Static Method

Here, we create three static methods to perform addition, subtraction, and multiplication.

class Employee:
     
    def func_message(self):
        print('Welcome to Python Programming')

    @staticmethod
    def func_addition(a, b):
        return a + b
 
    @staticmethod
    def func_multiply(a, b):
        return a * b
 
    @staticmethod
    def func_subtract(a, b):
        return a - b
 
print(Employee.func_addition(10, 20))
print(Employee.func_addition(15, 28))
 
print(Employee.func_multiply(2, 3))
print(Employee.func_multiply(5, 7))
 
print(Employee.func_subtract(20, 75))
print(Employee.func_subtract(1600, 249))
Python Static Method 6