×

Python Tutorial

Python Basics

Python I/O

Python Operators

Python Conditions & Controls

Python Functions

Python Strings

Python Modules

Python Lists

Python OOPs

Python Arrays

Python Dictionary

Python Sets

Python Tuples

Python Exception Handling

Python NumPy

Python Pandas

Python File Handling

Python WebSocket

Python GUI Programming

Python Image Processing

Python Miscellaneous

Python Practice

Python Programs

Python Static Methods (with Examples)

By Sapna Deraje Radhakrishna Last updated : December 21, 2024

Python static methods

Python static methods are class methods that are similar to the class-level method but static methods are bound to a class rather than the instances of the class. Static methods are called through the class (i.e., with the class name), thus there is no need to create an instance in the class to call them.

Creating Python static methods

Python static methods can be created by using the two different ways using staticmethod() method or @staticmethod decorator. Let's discuss them in detail.

1. Using staticmethod() method

The simple and popular approach to create static methods in Python is that you can use the staticmethod() method which is an inbuilt function and returns the static method for the given function.

Example to create Python static method using staticmethod() method

# Class definition
class Multiply:
    # Method to perform multiplication
    # two numbers
    def perform(x, y):
        return x * y

# Create perform() as static method
Multiply.perform = staticmethod(Multiply.perform)

# Take numbers
x = 4
y = 5

# Call static method
print("Multiplication of", x, "and", y, "is:", Multiply.perform(x, y))

Output

Multiplication of 4 and 5 is: 20

2. Using @staticmethod decorator

Another way to create Python static method is that you can use the @staticmethod decorator which is an inbuilt decorator used for defining static methods in the Python class.

Example to create Python static method using @staticmethod decorator

# Class definition
class Multiply:
    # sttaic method to perform multiplication
    # two numbers
    @staticmethod
    def perform(x, y):
        return x * y

# Take numbers
x = 4
y = 5

# Call static method
print("Multiplication of", x, "and", y, "is:", Multiply.perform(x, y))

Output

Multiplication of 4 and 5 is: 20

Note: The @staticmethod is more pythonic way to declare a method to be a static method.

Call Python static method from another class method

To call a Python static method from another method, you need to define a method by passing its first argument cls, and then call the static method using the cls. Consider the below program.

Example

# Class definition
class Multiply:
    # sttaic method to perform multiplication
    # two numbers
    @staticmethod
    def perform(x, y):
        return x * y

    # Calling static method inside
    # the class method
    def myfun(cls, m, n):
        return cls.perform(m, n)

# Take numbers
x = 4
y = 5

# Create an instace of the class
mul = Multiply()
# Call the class method
print(mul.myfun(x, y))

Output

20

Advantages of Python static methods

The following are the advantages of using Python static methods:

  • No class instance required: The static methods are accessed directly with the class name. So, you don't need to create an instance of the class to access the static methods.
  • Readability: Static methods are useful to increase the readability of the code. The related functionality can be grouped inside the static methods, in this way they can be accessed/modified easily.
  • Encapsulation: Using the static methods, encapsulation can be achieved, as the static method allow users to access other static methods and static variables without using the class instances.
  • Instance level accessibility: Since static methods are accessed through the class. You can also access them by using the instances of the class.
Advertisement

Comments and Discussions!

Load comments ↻


Advertisement
Advertisement



Copyright © 2024 www.includehelp.com. All rights reserved.