Question 25
What is the output of the following code?
Hint
Trace the values of f, a, and b inside bar() carefully.
f seems to represent a reference to a function, whilst a and b, on first glance, may be integer values.
Solution
Given x = bar(lambda a,b:a*b, bar(lambda a,b:a+b, a, b), bar(lambda a,b:a-b, a, b)),
x is the result of f(a, b) where f = lambda a, b: a * b, a = bar(lambda a,b:a+b, a, b), and b = bar(lambda a,b:a-b, a, b).
Function f is? |
a |
b |
|---|---|---|
lambda a, b: a * b |
bar(lambda a,b:a+b, a, b) |
bar(lambda a,b:a-b, a, b) |
For parameter a = bar(lambda a,b:a+b, a, b), obtain the result of plugging in values of global scope a and b into lambda a, b: a + b \(\Rightarrow\) 2 + 3 = 5.
For parameter b = bar(lambda a,b:a-b, a, b), obtain the result of plugging in values of global scope a and b into lambda a, b: a - b \(\Rightarrow\) 2 - 3 = -1.
We now have:
Function f is? |
a |
b |
|---|---|---|
lambda a, b: a * b |
5 |
-1 |
f(a, b) = (lambda a, b: a * b)(5, -1) = 5 * -1 = -5.