0

Can someone explain this code to me. I was expecting that it will throw error, but the output is 30.

https://code.sololearn.com/caTR7ugblr5F/?ref=app def x(n): return lambda y: n + y F=x(25) print(F(5))

30th Sep 2020, 5:18 AM
Gbadegesin Muhammed
Gbadegesin Muhammed - avatar
2 Respostas
+ 3
F = x(25) Here you are telling Python "Hey Python, when you see F, replace it with a call to function "x" and pass 25 as argument during the call." So, when you do F(5), Python replaces F with a call to function "x". Then in Python's view F(5) Becomes this x(25)(5) 25 is argument for call to function "x", and 5 is argument for the lambda returned by function "x".
30th Sep 2020, 6:23 AM
Ipang
+ 5
F=x(25)=lambda y:25+y F(5) =25+5 =30
30th Sep 2020, 6:25 AM
Oma Falk
Oma Falk - avatar