Imagine you wanted to be able to print all the results from calling a function.
However, you do not want to modify it but get a universal tool suitable for use with any functions instead. Let us implement such a function, which we will use a higher-order function for:
def printing(function):
def inner(*args, **kwargs):
result = function(*args, **kwargs)
print('result =', result)
return result
return inner
def add_one(x):
return x + 1
add_one = printing(add_one)
y = add_one(10)
# => result = 11
y
# 11
First, let us deal with the printing
function. This function creates an inner closure that: