+ 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)))
11 Respostas
+ 4
You can save it on the Playground and with some print statements between see why.
+ 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 ^^
+ 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
+ 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?
+ 1
visph Oh, sorry for that; I was just confused with your explanation. I've made another Q&A thread related to this.
+ 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 ;)
0
Why lambda?
0
because lambdas are anonymous onelined function, and that is equivalent to:
def b(x):
return x*4
0
owwh.. Thank you..
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 ^^
- 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.