Mastering Method Types With the OOP Pizza Example – Real Python (original) (raw)

In this lesson you’ll write a basic Pizza class, which is later used to demonstrate some use cases for the different method types.

00:00 Okay. So, this is what I came up with: the classical pizza example for teaching object-oriented programming. So what I’ve done here is I defined this really simple Pizza class.

00:10 It’s got a constructor that takes some arbitrary ingredients object—we’re just going to assume it’s some kind of list or container with these ingredients—and then I also put a .__repr__() on it so we can nicely format it as a string. And in here, if you’re wondering what that is—so, that is the new format strings in Python 3.6, which are really awesome, so I highly encourage you to try that out.

00:31 You could also just use regular format strings, of course.

00:35 So, okay. Basically, what I did here, is I created this Pizza class and now we can use it to create Pizza objects. And so, if I’m not mistaken, that’s a Margherita?

00:47 My wife’s Italian—you would probably kick my ass if I got that wrong—but I think that’s a Margherita. Well, what you’ve seen here is that we can create these Pizza objects, but as we create more and more complicated pizzas—ham,

01:04 with like a prosciutto or something, I don’t know. Maybe we need some mushrooms on that, as well, right? And you can already tell I’m struggling with the naming here, right?

01:14 I can create all of these wonderful pizzas here, but I need to remember all of these ingredients. So now, it wouldn’t be too much of a stretch to actually solve this problem with a static method.