Now that you have learned when to use class methods, the question arises when to use static methods.
The current lesson shows you an example, where you could make use of a @staticmethod
.
Furthermore, the code used in this lesson is provided below, so that you can copy and paste it to try it out yourself.
>>> import math
>>>
>>> class Pizza:
... def __init__(self, radius, ingredients):
... self.ingredients = ingredients
... self.radius = radius
...
... def __repr__(self):
... return f"Pizza({self.ingredients})"
...
... def area(self):
... return self._circle_area(self.radius)
...
... @staticmethod
... def _circle_area(r):
... return r ** 2 * math.pi
...
>>>
>>> Pizza(4.5, ["cheese"])
Pizza(['cheese'])
>>>
>>> Pizza(4.5, ["cheese"]).area()
63.61725123519331
prashant23 on July 9, 2019
Thanks for the nice tutorial. So,the static method can be considered to write certain helper functions?