Advanced Python Development Course
Chapter
>
Level
Advanced Classes
Super Function
Objective
Gather various meats then cook and serve them in different ways by creating objects with super functions.
The refrigerator has various meats that you can cook and serve with the onions you prepared with oil in the previous level. The meats can be cooked in different ways and up to different temperatures/rarities, you can achieve this be creating objects from classes, much like in previous levels but can be enhanced using Super Functions.
The super() function is used in a similar fashion to method overriding, the difference is that by using this function you can run the parent’s class function in addition to your own code in the child class instead of completely overriding the function.
# Parent Class
class cooked_meat:
type = ""
def cook_meat(self):
player.speak("Cooking the %s meat with oil
and onions." % self.type)
player.place("meat")
player.collect("cooked %s" % self.type)
# Child Class
class ready_meat(cooked_meat):
rarity = ""
def cook_meat(self):
super().cook_meat()
player.speak("The meat is cooked %s" % self.rarity)
Inside the child class function cook_meat(), the super() function is used. Since cook_meat() is also a function used in the parent class, the custom function would normally override the inherited code due to method overriding but by using the super() function you can execute code in the parent class as well as the new code you write in the child class.
First walk to the refrigerator and collect the four (4) pieces of meat you’re going to use to cook. Each of the four (4) meats is of a different type, those being : "pork" , "beef" , "lamb" and "buffalo" .
Once collected walk to the light X mark and create objects of each of the meats in order to cook them and for each meat object, set the type of meat and rarity you would like to cook them, for example:
pork_meat = ready_meat() pork_meat.type = "pork" pork_meat.rarity = "medium" pork_meat.cook_meat()
Rarity is the cooking temperature you would like a meat to be cooked at, have the meats cooked up to the following rarities: "pork" cooked "medium", "beef" cooked "well done" , "buffalo" cooked "medium rare", and "lamb" cooked "rare" . At the X mark use the child class function cook_meat() after setting the type and rarity of the meat as shown in the example above.
Once the meats have been cooked walk to the dark X mark on the yellow carpet and use the collect() function to grab both "salt" and "pepper" from the pantry. Create a list named ingredients and place the two picked up ingredients, like this: ingredients = ["salt","pepper"] .
With the extra ingredients you can now serve the the meat with plates on the table. Create a list for each of the cooked meats with the following names: served_pork , served_beef , served_lamb and served_buffalo Append to these lists the meats you cooked along with the ingredients list by creating a nested list, for example:
served_pork = [] served_pork.append("cooked pork") served_pork.append(ingredients)
Once the list have been set up walk to the gold X marks and use the place() function to serve the meat on the plates that are on the counters. On the top gold X mark, face the table, place() the: served_pork and served_buffalo. On the bottom gold X mark, face the table, place() the: served_lamb and served_beef. For example: player.place(served_pork) , once all meats are served you will have completed the level.