Advanced Python Development Course
Chapter
>
Level
Advanced Classes
Class Polymorphism
Objective
Finish preparing some wine mixtures so they can be ready for processing by using class polymorphism.
There are certain types of specialty wine that need more preparation than others and require extra additions before they can be mixed and aged into proper wine. To achieve this we must use class polymorphism, this is a way to use optimize the use of objects created using classes. This is done by having the internal class functions share properties so they can all be called up the same way despite being from different classes.
In the code editor you will have set up four (4) classes which include four different types of wine: grape , cherry , strawberry and mango . Each class will have two functions named add() and take_action() , despite the fact that these functions are structured the same way the contents of these functions can be different. Each of the classes need to have their take_action() function populated with a material to be placed.
To start off, walk to the light X mark and face the desk to read the note using the read() function. Make note of the information written which will tell you what needs to be added to the take_action() function of each class. For example, on the grape class needs "white oak" to be added using the place() function as illustrated below:
# Class for making grape wine
class grape:
def __init__(self,item):
self.name = "grape"
self.item = item
# Adds materials
def add(self):
player.speak("%s are added to the %s mixture." % (self.item, self.name))
# Take actions
def take_action(self):
player.place(self.name)
# New code added
player.place("white oak")
Once all materials that need to be placed are added to the class functions walk to the gold X mark and grab ingredients that need to be placed in the wine mixtures. Use the collect() function to grab a list constant named ingredients , and check the contents using the speak() function.
Walk to the bottom of the map and collect the four (4) bags of fruit and berries, so you may have the necessary components to finish the wine mixture.
Now that everything has been collected it’s time to add the all materials to finish the wine mixtures. Create objects of the classes and populate their argument, there should be four (4) objects: grape_mix , cherry_mix , strawberry_mix and mango_mix .
On each object set the class that corresponds with the name and add the item that needs to be mixed in in accordance with the list you collected. For grape there’s "nothing" added, for cherry add "pure water" , for strawberry add "sugar" and for mango add "spice" . As an example a defined object should look like this: grape_mix = grape("nothing")
Once objects have been created it’s time to put class polymorphism to use, walk to the dark X mark under the red carpet, face the barrel and use the loop set in the code editor. The loop will allow you to navigate to each wine barrel in the bottom row and have you add the elements needed to complete their mixtures.
The red carpet needs the strawberry_mix , the purple carpet needs the cherry_mix , the green carpet needs the grape_mix and the orange carpet needs the mango_mix. Add the elements to the loop in the order they are presented in the map from left to right. For example:
for mix in (strawberry_mix, mango_mix, cherry_mix): mix.add() mix.take_action() player.turn_right() player.move_forward(2) player.turn_left()
As in the example the mixes correspond to how the carpets are placed from left to right, meaning: red , orange , purple correspond to strawberry_mix, mango_mix, cherry_mix. Once sequence is complete, walk to the dark X mark on the green carpet, face the barrel, and do the same thing with the second loop that is set up in the code editor. This time have it be right to left with the color of the carpets ( green , purple , orange ) as shown in the map in order to complete the level.