static-methods-in-python

In Python, there are two methods to establish a Static Method, namely utilizing the static method() function and the @staticmethod decorator

This article will detail the construction of static methods through these two methods and present illustrations to facilitate comprehension.

Contents:

Python Static Method utilizing @staticmethod decorator

A Static method is one that is associated with the class preceding an instance. It does not accept self or cls as its first argument. Static methods are established through the @staticmethod decorator

Essential facts about static methods:

  • Cannot access instance (self) or class (cls): Static methods lack the ability to access or alter instance-specific or class-specific information. They function similar to ordinary functions, but are linked to the class for organizational reasons.
  • Established using @staticmethod decorator: Static methods are created using the @staticmethod decorator, succeeded by the method definition.
  • Invoked on the class or its instance: Static methods can be invoked directly from the class or an instance of that class.

Syntax:

class MyClass:
    @staticmethod
    def my_static_method(arg1, arg2, ...):
        print(f"...")

Example: This example illustrates the application of the @staticmethod decorator in Python.

class Calculator:
    @staticmethod
    def add(x, y):
        return x + y

    @staticmethod
    def multiply(x, y):
        return x * y

# Invoking static methods on the class
result_add = Calculator.add(4, 8)
result_multiply = Calculator.multiply(4, 8)
print(f"Sum: {result_add}, Product: {result_multiply}")

Output:

Unlocking the Power of Static Methods in Python

Python Static Method utilizing staticmethod() function

The staticmethod() is a built-in library function in Python employed to create a static method. It takes a method as an argument and transforms it into a static method. This approach is advantageous for utility functions that execute operations connected to the class but do not require access to or modification of its attributes.

Syntax

class MyClass:
    @staticmethod
    def my_static_method(param1, param2):
        # Implementation of the method
        print(param1, param2)

Example: This example demonstrates the utilization of the staticmethod() function in Python.

class Calculator:
    def add(x, y):
        return x + y
    def multiply(x, y):
        return x * y

    # Convert methods to static methods manually
    add = staticmethod(add)
    multiply = staticmethod(multiply)

# Calling static methods on the class
result_add = Calculator.add(5, 3)
result_multiply = Calculator.multiply(5, 3)
print(f"Sum: {result_add}, Product: {result_multiply}")

Output:

Unlocking the Power of Static Methods in Python

When to Utilize

Utilize @staticmethod decorator:

  • When constructing a class and you wish to explicitly define a static method.
  • This approach is more intelligible and the standard method in Python.

Utilize staticmethod() function:

  • If your code dynamically builds or alters methods, or if you seek to convert a function to a static method after its definition.

Conclusion

In Python, both the @staticmethod decorator and the staticmethod() function are employed to define static methods, which are associated with the class rather than its instances. The @staticmethod decorator is preferred and widely adopted due to its straightforwardness and clarity when defining static methods. Conversely, staticmethod() provides flexibility for adaptively converting functions into static methods, though it is employed less frequently. Both approaches serve the same function, but the decorator is generally regarded as the more Pythonic and comprehensible selection.

FAQ’s

The article Static methods in Python first appeared on Intellipaat Blog.


Leave a Reply

Your email address will not be published. Required fields are marked *

Share This