Can anyone explain me Why this code give Output 80? and How does this return function works? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Can anyone explain me Why this code give Output 80? and How does this return function works?

def Foo(n): def multiplier(x): return x*n return multiplier a= Foo(4) b=Foo(4) print(a(b(5)))

10th Jul 2021, 6:54 AM
Moon
Moon - avatar
11 Answers
+ 4
You can save it on the Playground and with some print statements between see why.
10th Jul 2021, 6:57 AM
JaScript
JaScript - avatar
+ 4
I guess that you badly indent the code while copying it... it must rather be: def Foo(n): def multiplier(x): return x*n return multiplier a = Foo(4) b = Foo(4) print(a(b(5))) 'Foo' function get is argument as 'n' variable, and create then return 'multiplier' function... both 'n' and 'multiplier' are stored in the scope of 'Foo' function, so 'Foo' function can access 'n' value related to the scope created at 'Foo' call runtime... so 'a' store a function wich return x*4 and 'b' store another function wich return x*4 also (if argument provided to Foo was different, the multplier operand would have reflect that value), where 'x' is the argument expected by these functions... so, that's equivalent to doing: def a(x): return x*4 or even: b = lambda x: x*4 then, b(5) return 5*4 == 20 and a(b(5)) is equivalent to a(20) and return 20*4 == 80 print(a(b(5))) then output 80 ^^
10th Jul 2021, 8:04 AM
visph
visph - avatar
+ 2
It's a closure. To make closure function you need 3 step 1.Must be create internal function 2.The maked function should have access to its own enclosing namespace variables 3.Return the internal function Look at the code for a better understanding https://code.sololearn.com/c7MDn4R8Oo5d/?ref=app
10th Jul 2021, 8:06 PM
Amir
Amir - avatar
+ 1
visph Why doesn't the local variables 'n' and 'multiplier' get cleared? From my understanding, "def multiplier(x): return x*n" defines a function object in the heap that returns the product of the integers contained in the parameter 'x' and the variable 'n' (which isn't immediately replaced by 4 at the moment of declaration, right?). Now, 'b' contains the reference to the function object in the heap. The function was declared to return the value of 'x' multiplied 'n', but where is the local variable 'n' after the function exits? Also, why isn't the multiplier variable cleared, as it's a local variable (reference) of the Foo() function?
10th Jul 2021, 7:03 PM
Calvin Thomas
Calvin Thomas - avatar
+ 1
visph Oh, sorry for that; I was just confused with your explanation. I've made another Q&A thread related to this.
10th Jul 2021, 7:34 PM
Calvin Thomas
Calvin Thomas - avatar
+ 1
Calvin Thomas you can even mention me in your new thread, and I will be happy to answer you, if someone do not answer before me ;)
10th Jul 2021, 7:43 PM
visph
visph - avatar
0
Why lambda?
10th Jul 2021, 11:30 AM
Moon
Moon - avatar
0
because lambdas are anonymous onelined function, and that is equivalent to: def b(x): return x*4
10th Jul 2021, 11:32 AM
visph
visph - avatar
0
owwh.. Thank you..
10th Jul 2021, 1:31 PM
Moon
Moon - avatar
0
Calvin Thomas stop testing me in other thead with question wich are above the level of the initial question... if you really want answer, so post your own question thread, and someone will explain you, or better make your own research on internet ^^
10th Jul 2021, 7:08 PM
visph
visph - avatar
- 2
Why don’t you try on Visual Studio Code with a debugger to see why the return gives you a big number. You have 2 inputs as far as I can see and ask for x*n go be returned back first. Try debugging on VSCode as mentioned. It can show how each line is being processed.
11th Jul 2021, 9:34 PM
Crow
Crow - avatar