Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages
wb_sunny

@staticmethod in Python

The @staticmethod decorator

Python @staticmethod decorator is used to label a class method as a static method, which means that it can be called without instantiating the class first. It simply defines a normal function that is logically contained in the class for readability purposes. Here, we do not need to pass the class instance as the first argument via self, unlike other class functions.

class Student():
       def __init__(self, mark):
           self.mark = mark
  
       @staticmethod
       def find_min(mark):
           return min(mark, 100)
  
print(Student.find_min(20))

Output

20


Accessing via Class Instances

Static methods can also be accessed via class instances or objects. For example :

class Bank():
       def __init__(self, balance):
           self.balance = balance
  
       @staticmethod
       def find_interest(loan_money, interest_rate):
           return loan_money * (1 + interest_rate)
  
bank = Bank(1000)
print(bank.find_interest(bank.balance, 0.3))

Output:

1300

Overriding static methods

Static method definitions are unchanged even after any inheritance, which means that it can be overridden, similar to other class methods.

class ATM(Bank):
    def __init__(self, balance):
        super().__init__(balance)
    
    @staticmethod
    def find_interest(loan_money):
        return loan_money * 1.4

atm = ATM(1000)
print(atm.find_interest(atm.balance))

Output

1400

@staticmethod vs @classmethod

Class methods are called via the Class containing it, rather than from an instance. Which is why a classmethod is defined as class_method(cls, param1, param2), which does not contain self, which means that it cannot be called using a class instance. Static methods can be called from both a class instance as well as from a Class.


@staticmethod vs instance methods

Instance methods can only be called from a class instance, which is why any instance method is of the forminstance_method(self, param1, param2), where the self keyword signifies the class instance calling the method. @staticmethod can be called from both a class instance as well as from a Class.

Staticmethods
Comparison between @classmethod, @staticmethod and instance methods

Here is an example to put it all together.

class Bank():
    def __init__(self, balance):
        self.balance = balance

    @staticmethod
    def find_interest(loan_money, interest_rate):
        return loan_money * (1 + interest_rate)

    @classmethod
    def find_interest_classmethod(cls, loan_money):
        return loan_money * 1.4

    def find_interest_instancemethod(self, loan_money):
        if self.balance <= 100:
            return loan_money * 1.2
        else:
            return loan_money * 1.5


class ATM(Bank):
    def __init__(self, balance):
        super().__init__(balance)

    @staticmethod
    def find_interest(loan_money):
        return loan_money * 1.3


atm = ATM(1000)
print('Using staticmethod ->', atm.find_interest(atm.balance))
print('Using classmethod from the parent class ->', Bank.find_interest_classmethod(atm.balance))
print('Using classmethod from the inherited subclass ->', ATM.find_interest_classmethod(atm.balance))
print('Using a regular instance method ->', atm.find_interest_instancemethod(atm.balance))

Output:

Using staticmethod -> 1300.0
Using classmethod from the parent class -> 1400.0
Using classmethod from the inherited subclass -> 1400.0
Using a regular instance method -> 1500.0

Conclusion

Python static methods belong to the class. They are useful in creating functions that don’t need instance variables to work. This article showed us the use cases of @staticmethod, and how they compare with other such decorators, namely the @classmethod decorator. We also gave a comparison between static methods, class methods, and regular instance methods, and showed how they can be used to make the class code easier to maintain.

References

Python Official Documentation: https://docs.python.org/3/library/functions.html#staticmethod