staticmethod() Syntax The syntax
of staticmethod() is: staticmethod(function) staticmethod() Parameters The staticmethod() method takes only one parameter: function
- – function that needs to be converted to
a static staticmethod()
method Return type The staticmethod
()
returns a static method
for a function passed as a parameter.
What is a static method?
Static methods, like class methods, are methods that are bound to a class rather than to its object.
They do not require the creation of a class instance. Therefore, they do not depend on the state of the object.
The
difference between
a static method and a class method is: The
- static method knows nothing about the class and only deals with parameters. The class method
- works with the class since its parameter is always the class itself.
They can be called both by class and by their object.
Class.staticmethodFunc() or even Class().staticmethodFunc() Example 1:
Create a static method using the
staticmethod() class Math: def addNumbers(x, y): return x + y # create addNumbers static method Mathematics.addNumbers = staticmethod(Mathematics.addNumbers) print(‘The sum is:’, Mathematics.addNumbers(5, 10))
Output
The sum is: 15
When are static methods used?
1. Grouping utility function
in a class Static methods
have a limited use case because, like class methods or any other method within a class,
they cannot access the properties of the class itself.
However, when you need a utility function that doesn’t have access to any properties of a class, but it makes sense for it to belong to the class, we use static functions.
Example 2: Create a utility function as a static method class
Dates: def __init__(self, date): self.date = date def getDate(self): return self.date @staticmethod def toDashDate(date): return date.replace(“/”, “-“) date = Dates(“15-12-2016”) dateFromDB = “15/12/2016” dateWithDash = Dates.toDashDate(dateFromDB) if(date.getDate() == dateWithDash): print(“Equal”) else: print(“Unequal”)
Output
Equal
Here, we have a Dates class that only works with hyphenated dates. However, in our previous database, all dates were present in bars.
To convert forward slash dates to dash dates, we’ve created a utility function for DashDate within Dates.
It’s a static method because you don’t need to access any Dates properties and only require the parameters.
We can also create toDashDate outside the class, but since it works only for dates, it makes sense to keep it inside the Dates class.
2. Have a single implementation
Static methods are used when we don’t want subclasses of a class to change/override a specific implementation of a
method.
Example 3: How does inheritance work with the static method?
class Dates: def __init__(self, date): self.date = date def getDate(self): return self.date @staticmethod def toDashDate(date): return date.replace(“/”, “-“) class DatesWithSlashes(Dates): def getDate(self): return Dates.toDashDate(self.date) date = Dates(“15-12-2016”) dateFromDB = DatesWithSlashes(“15/12/2016”) if(date.getDate() ==
dateFromDB.getDate
()): print(“Equal”) else: print(“Unequal”)
Output
Equal
Here, we wouldn’t want the DatesWithSlashes subclass to override the toDashDate static utility method because it only has one use, i.e. changing date to dash-dates.
We could easily use the static method to our advantage by overriding the getDate() method on the subclass to make it work well with the DatesWithSlashes class.