[SOLVED] Confused with Python functions...
Consider this code snippet: def foo1(x1): def foo2(x2): return x1 * x2 return foo2 a = foo1(3) print(a(4)) # Prints 12 as the output How does this even work? Here's what I understand from this code: """ First, the function object 'foo1' gets initialized in the heap. << Q.1) Is 'foo2' initialized during the creation of 'foo1' or only when 'foo1()' gets called? >> Now, 'foo1' gets called. It returns the reference to the function object 'foo2' and variable 'a' stores it. << Q.2) Does the function 'foo2' get created to return 3 * 'x2' or 'x1' * 'x2'? What I mean by this is; consider another code snippet: foo = lambda x: return t * x print(foo) t = 2 print(foo(3)) # Prints 6 Here, the function having its reference stored in the 'foo' variable is created in the heap to return the product of its parameter 'x' and the variable 't', which is created later; a variable named 't' is to be searched and substituted by the interpreter if found later in the code. Continued in the comments -- """