Python Static Method

We need static methods when the processing is at the class level but we need not involve the class or instances. Static methods are used when some processing is related to the class but does not need the class or its instances to perform any work.

Setting environmental variables, counting the number of instances of the class or changing an attribute in another class, etc. are the tasks related to a class. Such tasks are handled by static methods. Also, static methods can be used to accept some values, process them and return the result. In this case the involvement of neither the class nor the objects is needed.

Static methods are written with a decorator @staticmethod above them. Static methods are called in the form of classname.method().

A Python program to create a static method that counts the number of instances created for a class.

A Python program to create a static method that calculates the square root value of a given number.

A Python program to create a Bank class where deposits and withdrawals can be handled by using instance methods.