Mastering Method Types With the OOP Pizza Example
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.
Dan Bader RP Team on June 12, 2019
@Steven: Haha, all good points… time to work on a course on attribute validation :-)
Dan Bader RP Team on June 12, 2019
Just got this comment via the feedback form and I wanted to share it here because it raises a good point:
One thing that got my eyes on was how ingredient parameter is used in the constructor. This parameter is given in as a sequence argument (list) and for that reason it should be stored to the instance variable by copying it.
self.ingredients = list(ingredients)
Not particularly relevant to this tutorial but anyway might this may lead to an unexpected situations. At least novice python programmer’s should be aware of.
Lastly, I want to say thanks for the awesome site Real Python! I have used Python for years and still I constantly learn new things from the site. Real Python certainly has raised my Python knowledge yet to another level.
I agree it would be good to make a copy of the ingredients list when assigning it to the instance variable, so thanks for that. Potentially we might event want to do a deep copy here.
balakumaranrk on Feb. 9, 2020
can please you explain in detail what the method def repr(self) is used for ?
Dan Bader RP Team on Feb. 9, 2020
Check out this video course as a follow up: Pythonic OOP String Conversion: __repr__
vs __str__
Become a Member to join the conversation.
Steven Rowland on June 12, 2019
You need fresh basil on a Margarita pizza. And cheese? What kind of cheese? These are important details. You need a pizza validator.