def compose2(func1, func2, arg):
"""Returns the result of applying func1 to the result of applying func2 to arg.
func1: a function of one argument
func2: a function of one argument
arg: something that can be passed to func2 as an argument
"""
return func1(func2(arg))
compose2(len, str, 10+20)this first step is equivalent to the following (temporary) assignments
func1 = len func2 = str arg = 10+20Note that the values of the parameters are the values of the expressions on the right-hand sides, that is, the function
len in the first case and 30 in the third case.
return len(str(10))Because this is a
return statement, the resulting value is returned by the function call
>>> compose2(len, str, 10+20) 2
>>> func1
Traceback (most recent call last):
File "<pyshell#13>", line 1, in <module>
func1
NameError: name 'func1' is not defined
Answers to problems in these notes