Why does `return g` with no parentheses make a chained call like `make_prod(2)(3)` work, when `return g()` never could?
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 drew an integer, a float, and a function in three identically-sized memory boxes to make the point that nothing about the function row is special. Check that Python agrees.
Two values, radius and pi, are given to you. Write a function named is_odd that takes one number and gives back whether that number is odd. Then assign to report a four-element list holding:
radius's type, as a stringpi's type, as a stringis_odd's type, as a stringis_odd gives for 7(Vocabulary: type(obj) hands back an object's type, and every type carries a .__name__ attribute holding its own name as a string. % is the remainder operator.)
A function definition as an object in memory
Record that a def binds a name to a function object exactly as r = 5 binds a name to an integer, and that the body runs only at a call.
Aliasing with my_func = is_even
Write the assignment without parentheses, draw the two names pointing at one object in memory, and record that a call through either name runs the same body.
Returning the name g rather than a call
Write make_prod with its inner g, mark the missing parentheses on return g, and record what the line make_prod(2)(3) becomes if the returned value were 10 instead.
Environment trace of make_prod(2)(3)
Step through the global scope, the make_prod frame with a mapped to 2, and the g frame with b mapped to 3, resolving a from the scope where g was defined and printing 6.