Tutorial Gateway

  • C
  • C#
  • Java
  • Python
  • SQL
  • MySQL
  • Js
  • BI Tools
    • Informatica
    • Talend
    • Tableau
    • Power BI
    • SSIS
    • SSRS
    • SSAS
    • MDX
    • R Tutorial
    • Alteryx
    • QlikView
  • More
    • C Programs
    • C++ Programs
    • Python Programs
    • Java Programs

Python Static Method

by suresh

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()
Python Static Method 1

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()
Python Static Method 2

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))
Python Static Method 3

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 Static Method 4

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 Static Method 5

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

Placed Under: Python

  • Download and Install Python
  • Python Arithmetic Operators
  • Python Assignment Operators
  • Python Bitwise Operators
  • Python Comparison Operators
  • Python Logical Operators
  • Python If Statement
  • Python If Else
  • Python Elif Statement
  • Python Nested If
  • Python For Loop
  • Python While Loop
  • Python Break
  • Python Continue
  • Python Dictionary
  • Python datetime
  • Python String
  • Python Set
  • Python Tuple
  • Python List
  • Python List Comprehensions
  • Python Lambda Function
  • Python Functions
  • Python Types of Functions
  • Python Iterator
  • Python File
  • Python Directory
  • Python Class
  • Python classmethod
  • Python Inheritance
  • Python Method Overriding
  • Python Static Method
  • Connect Python and SQL Server
  • Python SQL Create DB
  • Python SQL Select Top
  • Python SQL Where Clause
  • Python SQL Order By
  • Python SQL Select Statement
  • Python len Function
  • Python max Function
  • Python map Function
  • Python print Function
  • Python sort Function
  • Python range Function
  • Python zip Function
  • Python Math Functions
  • Python String Functions
  • Python List Functions
  • Python NumPy Array
  • NumPy Aggregate Functions
  • NumPy Arithmetic Operations
  • Python Numpy Bitwise operators
  • Numpy Comparison Operators
  • Numpy Exponential Functions
  • Python Numpy logical operators
  • Python numpy String Functions
  • NumPy Trigonometric Functions
  • Python random Array
  • Python numpy concatenate
  • Python numpy Array shape
  • Python pandas DataFrame
  • Pandas DataFrame plot
  • Python Series
  • Python matplotlib Histogram
  • Python matplotlib Scatter Plot
  • Python matplotlib Pie Chart
  • Python matplotlib Bar Chart
  • Python List Length
  • Python sort List Function
  • Python String Concatenation
  • Python String Length
  • Python substring
  • Python Programming Examples
  • C Tutorial
  • C# Tutorial
  • Java Tutorial
  • JavaScript Tutorial
  • Python Tutorial
  • MySQL Tutorial
  • SQL Server Tutorial
  • R Tutorial
  • Power BI Tutorial
  • Tableau Tutorial
  • SSIS Tutorial
  • SSRS Tutorial
  • Informatica Tutorial
  • Talend Tutorial
  • C Programs
  • C++ Programs
  • Java Programs
  • Python Programs
  • MDX Tutorial
  • SSAS Tutorial
  • QlikView Tutorial

Copyright © 2021 | Tutorial Gateway· All Rights Reserved by Suresh

Home | About Us | Contact Us | Privacy Policy