Advanced Python Development Course
Chapter
>
Level
Decorators
Chaining Decorators
Objective
Decorate the roof with colored flags by chaining decorators.
The roof has some fresh air after being cooped up inside that kitchen for so long, some of the lines don’t have flags. It would be a good idea to set them up while you’re here, however it can be tricky given you need various flags to set up a line. You can accomplish that by chaining decorators.
Decorators are chained by doubling up on the @ signs, that is, fixing multiple decorators on a single function. For example, this allows you to sandwich a function between two decorator functions:
# List to add flags to a line
line = []
# Decorator adding a red flag
def red_flag(func):
def add_flag():
line.append("red")
player.speak("Two red flags added to the line")
func()
line.append("red")
return add_flag
# Decorator adding a blue flag
def blue_flag(func):
def add_flag():
line.append("blue")
player.speak("Two blue flags added to the line")
func()
line.append("blue")
return add_flag
# Decorated Function adding line and middle flag
@blue_flag
@red_flag
def middle_green():
line.append("green")
player.speak("A green flag has been added to the line")
The code above illustrates a function with chained decorators attached, these decorators are weaved into the function so that on the edges of the line blue flags are placed, the inner layer has red flags placed and finally in the middle a green flag is placed. They are all appended to a list named line , this is the method that will be used to set up the flags on the lines.
Start off by walking to the gold X mark and using the read() function to check the instructions listed on the signpost. Here it will inform you of what flags to put where with there being three (3) different types of colored flags: "red" , "green" and "blue" . Each line will have all three flags, one (1) in the middle of the line, two (2) in the inner line and two (2) in the outer line, making a total of five (5) flags per line much like the premade line at the lower left side of the map.
Walk to the light X mark and use the collect() function to grab the "flags" in the crate. Follow this up by walking to the dark X marks and from left to right start putting the flags in the order that they described in the instructions you read earlier. Use the middle functions in order to achieve this, for example: middle_green() creates a flag line like this: ["blue", "red", "green" ,"red","blue" ] . The use the place() function to set the line using the line list, like this: place(line) .
Between each X mark clear the line list by using the clear() function, like this: line.clear() , this allows you to reuse the list without needing to create a new one for each line. The final X mark to the right requires a custom function:
@
@
def middle_custom():
line.append( Insert append )
player.speak("%s flag has been added to the line"
% ( insert flag ))
Use the two @ symbols set to add decorators to the function, follow this up by filling out the line append and color of the flag in the string. Do this in accordance to the instructions in the sign in order to complete the level.