Teacher's Guide

Chapter 6 / Level 4: Chaining Decorators

Teaching Objective:

  • Student understands more about the syntax pertaining to decorators and methods for chaining them together
  • Learns more uses and implementations for decorators and how decorator chains can be mixed and matched
  • Understands the versatility and optimization benefits of chaining decorators and it’s benefits

Guide

In this exercise we’re going to be working with chaining decorators, this is the process in which we attach multiple decorators to a single function and thus allowing you to add and combine the code from multiple sources. This is done by adding multiple @ symbols onto a single function.

For this level we’re going to be setting up lines of colored flags, these flags need to be put in a sequence. The sequence in the line is two (2) of the same colored flag in the outer parts of the line, two (2) of the same color in the inner part of the line and one (1) colored flag in the middle of the line. The colors of the flags you can place are: "red" , "green" and "blue" .

To add flags to a line first you must create a list named line , you will be using and reusing this list for all the lines of flags you place.

line = []

Set up the decorators for adding each combination of colored flags so you may wrap the functions and form a line.

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

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

@red_flag
@blue_flag
def middle_green():
  line.append("green")
  player.speak("A green flag has been added to the line")

Walk to the gold X mark and use the read() function to check and identify the combination of flag lines to set up from left to right.

await player.read()

Walk to the light X mark and collect() the flags in order to set up the lines.

player.collect("flags")

Use the proper decorated function at the correct X marks according to the sign. Once the line is set up, place() the line and once placed clear() the line list in order to reuse the list for the next line.

middle_green()
player.place(line)
line.clear()

Map overview and path

Map overview and path

Code Solution

import player

line = []

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

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
    
def green_flag(func):
  def add_flag():
        
    line.append("green")
    player.speak("Two green flags added to the line")
    func()
    line.append("green")
        
  return add_flag  

@red_flag
@blue_flag
def middle_green():
  line.append("green")
  player.speak("A green flag has been added to the line")

@red_flag
@green_flag
def middle_blue():
  line.append("blue")
  player.speak("A blue flag has been added to the line")

@green_flag
@blue_flag
def middle_red():
  line.append("red")
  player.speak("A red flag has been added to the line")

  
@blue_flag
@red_flag
def middle_custom():
  line.append("green")
  player.speak("A green flag has been added to the line")

player.turn_left()
player.move_forward()
await player.read()
player.turn_left()
player.turn_left()
player.move_forward(5)
player.collect("flags")
player.turn_left()
player.move_forward(2)
middle_green()
player.place(line)
line.clear()
player.turn_left()
player.move_forward(2)
player.turn_right()
middle_red()
player.place(line)
line.clear()
player.turn_left()
player.move_forward(2)
player.turn_right()
middle_blue()
player.place(line)
line.clear()
player.turn_left()
player.move_forward(2)
player.turn_right()
middle_custom()
player.place(line)
line.clear()
Chapter 6 / Level 4: Chaining Decorators | Teacher's Guide