Nested Functions II
Great job, you've just nested a function within another function. One other pretty cool reason for nesting functions is the idea of a closure. This means that the nested or inner function remembers the state of its enclosing scope when called. Thus, anything defined locally in the enclosing scope is available to the inner function even when the outer function has finished execution.
Let's move forward then! In this exercise, you will complete the definition of the inner function inner_echo() and then call echo() a couple of times, each with a different argument. Complete the exercise and see what the output will be!
Deze oefening maakt deel uit van de cursus
Introduction to Functions in Python
Oefeninstructies
- Complete the function header of the inner function with the function name
inner_echo()and a single parameterword1. - Complete the function
echo()so that it returnsinner_echo. - We have called
echo(), passing 2 as an argument, and assigned the resulting function totwice. Your job is to callecho(), passing 3 as an argument. Assign the resulting function tothrice. - Hit Submit to call
twice()andthrice()and print the results.
Praktische interactieve oefening
Probeer deze oefening eens door deze voorbeeldcode in te vullen.
# Define echo
def echo(n):
"""Return the inner_echo function."""
# Define inner_echo
def ____(____):
"""Concatenate n copies of word1."""
echo_word = word1 * n
return echo_word
# Return inner_echo
# Call echo: twice
twice = echo(2)
# Call echo: thrice
# Call twice() and thrice() then print
print(twice('hello'), thrice('hello'))