Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set the default subtitles language in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

More Pizza and When to Use Static Methods

Give Feedback

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?

EKELENNOROM on July 13, 2019

The code doesn’t work for python 3.6 and 3.7

Dan Bader RP Team on July 15, 2019

@EKELENNOROM: What’s the error message you’re seeing? Works just fine here on my end (3.6 and 3.7), see below:

→ bpython
bpython version 0.18 on top of Python 3.7.3 /Users/dbader/.pyenv/versions/3.7.3/bin/python3.7
>>> 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

Azian on July 31, 2019

The code runs fine for me on Python 3.7.4.

Zarata on April 20, 2020

Maybe “Pizza” somewhat obscures this one. “in another language” (ahem) static methods and even static classes of methods are fairly common for a large number of common functionalities. Maybe think “hand calculator” … the ones with all those cool engineering functions built in. But that does call to mind a question: when is it better to pack such things into a class that you have to instantiate first to get all the cool class methods, and when is it better to have a passle of statics? What are the underlying efficiencies and costs? Once upon a time, I think “static” had specialized memory implications … Not certain in Python much less any other language now.

Roy Telles on Aug. 17, 2020

Just wanna say it was a good point to make (and this actually may be the crux of static methods) in saying that the static method simplifies testing in that we don’t have to worry about instantiating our object first, and then passing in arguments to its functions.

Become a Member to join the conversation.