A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://realpython.com/lessons/more-pizza-and-when-use-static-methods/ below:

More Pizza and When to Use Static Methods (Video) – Real Python

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

Unlock This Lesson

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

Unlock This Lesson

More Pizza and When to Use Static Methods

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.

00:00 Up next, when to use static methods. It’s a little bit hard to come up with a really simple example here, but you know, I’m going to keep stretching the pizza thing here. So, okay. This is what I came up with. Basically, a static method doesn’t have access to the class or the object instance at all, all right? And now, that’s a pretty big limitation, but it’s also a really good signal to show that a particular method is really independent from everything else around it. So for example, if I flag this as a static method, it’s pretty clear that this method, it’s probably not going to change the object or the class in any way,

00:46 because it’s not really meant to have access to it. I mean, sure, you know, you could probably work around that and it could kind of work with a global variable, but in general, it’s a pretty good hint that this is a self-contained method, which has all kinds of benefits for later testing this thing and just kind of understanding the limitations of this method.

01:04 And so in some cases, it can really be helpful to make your code easier to maintain in the future, because you’re really communicating this intent where someone else reading this code can pretty clearly understand that in this case, our little ._circle_area() function here—it’s not going to modify the state of this object itself.

01:22 So let me walk you through this example real quick. So basically, what I’ve added here—well, I kind of changed the constructor around a bit, so now we’ve got a radius argument here for the pizza as well.

01:33 I forgot to update the .__repr__(), but you know, it doesn’t really matter for now. And then I added this .area() function. And, well, I could have just calculated the area of the pizza directly in here, but I wanted to have a static method in there.

01:46 And so basically, what I did there instead of calculating the area directly with an expression here, I’m just offloading that to the ._circle_area() function, and then the ._circle_area() function just takes an r, which is the radius, and uses the classic r ^ 2 * pi formula to calculate the circle area. So, you know, this is honestly kind of a simplistic example, and you usually wouldn’t implement it like that, but it goes to show the concept.

02:15 And now what happens here is I can instantiate a Pizza. So for example, this is a really sad pizza with like 4.5well, we don’t have a unit on that—let’s say meters. A giant pizza, cheese only.

02:31 And we’re going to create this object, this Pizza object, and then we’re going to call the .area() function on it, and it’s going to give us this result.

02:38 So, the way this is calculated is that .area() actually forks off all that work to the ._circle_area() function, and now the ._circle_area() function is actually completely independent, and I also used a single underscore (_) to mark it as not part of the public API of this class, but kind of an internal implementation detail. But nevertheless, now the cool thing is that we have this ._circle_area() function that is completely independent from the rest of the object, and it can’t actually modify the object’s state.

03:15 This is a pretty good hint for someone else reading the code and it also simplifies testing because when I write my tests for this, then I don’t have to worry about instantiating a Pizza object, making sure it doesn’t get modified, because the ._circle_area() helper—it can’t do that. Right?

03:34 And so I occasionally use static methods to communicate that intent and to keep my helper functions nice and clean and make sure that they’re not modifying the object’s state all over the place.

Course Contents


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4