Python Static Methods

Python – Static Methods

In this tutorial, we will explore the concept of static methods in Python.

Static methods are a way to define methods within a class that are not bound to the class instances. They can be called using the just the class name. Since they are not class instance dependent, they cannot access the state of a class instance.

How to define static methods in Python

They are defined using the `@staticmethod` decorator and can be called using the class name or the instance name.

@staticmethod function decorator transforms a method into a static method in a class.

Examples

1. Creating a Static Method in a Math utility Class

In this example, we’ll create a static method within a class that calculates the square of a number.

The square() method is defined as a static method using the @staticmethod decorator. In this case square() is a utility function for the MathUtils class.

Python Program

class MathUtils:
    @staticmethod
    def square(number):
        return number * number

num = 5
result = MathUtils.square(num)
print(f"The square of {num} is {result}.")
Run Code Copy

Output

The square of 5 is 25.

If you observe the program, in the statementresult = MathUtils.square(num) we have used the class name to call the static method. No instance of MathUtils class is created.

2. Using Static Method in Temperature converter class

Let’s create a static method within a class that converts temperature from Celsius to Fahrenheit.

The celsius_to_fahrenheit() method is a static method used for temperature conversion. It demonstrates a scenario where a utility function doesn’t need access to instance or class data.

Python Program

class TemperatureConverter:
    @staticmethod
    def celsius_to_fahrenheit(celsius):
        return (celsius * 9/5) + 32

celsius_temp = 25
fahrenheit_temp = TemperatureConverter.celsius_to_fahrenheit(celsius_temp)
print(f"{celsius_temp}°C is equal to {fahrenheit_temp}°F")
Run Code Copy

Output

25°C is equal to 77.0°F

Where are static methods used

Static methods in Python are used in scenarios where a method is logically related to a class but doesn’t depend on instance-specific or class-specific data. Some common use cases for static methods include:

  1. Utility Functions like conversion functions, mathematical calculations, or formatting functions.
  2. Helper Methods to help keep the code organized and separates concerns.
  3. Factory Methods to handle complex initialization logic without requiring the instantiation of the class itself.
  4. Alternate Constructors where you want to provide multiple ways to create an object of a class, you can define static methods to serve as alternate constructors. These methods can take different parameters or input formats to create instances.

Summary

In this tutorial of Python Classes and Objects, we have learnt about static methods in a class. We have seen how to define a static method in a class, what are the uses of a static method, and examples for static method.

Related Tutorials

Code copied to clipboard successfully 👍