What is Static Method in Python

Static methods are mthods that are bound to a class rather than its object. They do not require a class instance creation, therefore they are independent on the state of the object.
The staticmethod()
is a built-in function in Python which returns a static method for a given function.
Syntax: staticmethod(function)
class calculator: def add(x, y): return x + y calculator.add = staticmethod(calculator.add) print("Sum = ", calculator.add(2, 3))
Output:
Sum = 5
Static method knows nothing about the class and it just deals with the parameters.
Subscribe
Login
Please login to comment
0 Discussion