chocolate = cake()
This binds the identifier chocolate
to the object returned by invoking cake
- it returns a function object - therefore, chocolate is bound to that function object (chocolate
is bound to the function object pie
).
A side effect of invoking cake
is is that "beets" is printed.
cake()()
This invokes cake
, which returns a function object. This time the function object is not bound to a name. Upon returning we invoke the anonymous function object. The result is "beets" is printed from the call to cake
, and "sweets" is printed from the call to pie
. pie
also returns the string "cake", but that string is not bound or captured.
chocolate()
chocolate
is still bound to the function object returned by cake
when we did chocolate = cake()
. Now we are simply invoking the function object we captured earlier. Since we are not actually calling cake
now (we're only calling pie
), "beets" is not printed, but "sweets" is. Again, this returns the string "cake", but again, it is not bound or captured.
more_chocolate, more_cake = chocolate(), cake
this binds more_chocolate
to the object returned by calling chocolate
(which is the string "cake"). It also binds more_cake
to cake
. Not sure if you actually meant to do this - all this does is it binds more_cake
to the same function object that cake
is bound to, but you're not actually invoking any function here.