Advanced Python Development Course
Chapter
>
Level
Advanced Functions
Closures
Objective
Regulate the water supply in the farm by setting up and using closures.
There are four water tanks designed to irrigate the surrounding farmland, it’s about time they are switched up in order to make sure the correct amount of water is being distributed to the right places. In order to do this we can make the job a lot easier by using Closures.
Closures are nested functions designed to encapsulate data, this reduces the amount of variables you need to use and hides the data within the function so it can’t be manipulated externally in other words, closing up the function.
In order to regulate the water in the tanks we will create a nested function that works as multiplier. This function works in the same manner as a loop, the outer function named multiplier_of() has an argument that is multiplied by the argument of it’s inner function named multiplier().
# Nested Function
def multiplier_of(n):
def multiplier(number):
return number*n
return multiplier
Once set up we need to enclose the function by setting up closures, this prevents access to their values and solidifies their use.
# Set up Closure that multiplies any number by 2 multiply_by_2 = multiplier_of(2) # Uses closure to display a number on screen player.speak( multiply_by_2(4) ) # The number displayed is 8, the formula is [2 x 4 = 8]
The closure above is named multiply_by_2() and uses the nested function we created with a set argument, that being 2, which is the number the closure is set to multiply by. As a result any number we we add as an argument will be multiplied by two (2) and this attribute can not be changed due to being enclosed.
Moving onto regulating the water tanks, set up three (3) closures: multiply_by_2 , multiply_by_3 and multiply_by_5 . Manually set each of them up using the same format used in the example previously provided, like this: multiply_by_2 = multiplier_of(2) .
There is a dictionary constant in this level named tanks , as an index it holds six (6) colors: “red" , “blue" , “orange" , “purple", “yellow" and “green" . Each index holds a number representing how much water is flowing through a tank’s flank, this number will be used to regulate the tank by using the “multiply_by” closures previously discussed.
To facilitate this a function will be set up in the editor named regulate_water() , it takes two arguments: the color which represents the indexes outlined above and the multiplier which represents the multiplier closure to use. An example of the function in use is as follows: regulate_water(“red" , multiply_by_2 ) .
Walk to the dark X marks above colored carpets and face the tanks, use the regulate_water() function and set the color of the carpet as the first argument and the proper multiplier closure as the second. The multipliers to be used on each colored carpet are as follows:
multiply_by_2 - "red" and "green" multiply_by_3 - "blue" and "purple" multiply_by_5 - "orange" and "yellow"
Regulate the water on each of the tank’s flanks in order to complete the level.