If `doubler` and `g` name the very same object, why would anyone return a function instead of just computing the answer outright?
Short drills on what this video just taught. Write the code, run the checks, and reveal the answer only if you are stuck.
The video takes a chained one-liner apart and gives its first half a name. Do the same here: ask make_prod for the function that multiplies by 5, and catch what it hands back.
Assign that returned function to times5. Do not call it — times5 must end up holding the function object itself, not a number.
def make_prod(a):
def g(b):
return a * b
return gBinding the returned object to doubler
Trace doubler = make_prod(2) through the frame where a is 2 and g is defined, record what doubler holds afterwards, then run doubler(3) to val equal to 6.
Chained call versus intermediate variable
Write make_prod(2)(3) beside the two-line version, record that the two are equivalent and both produce 6, and note which form the video prefers.
Three uses for a function that returns a function
Record hiding helper functions inside another function's scope, delaying the final call so other code can run in between, and adding a tool for decomposition and abstraction in large programs.