Lambda issue | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Lambda issue

Please help in describing below code: a = (lambda x: x**3)((lambda x: x+2)(1)) print(a) Output is : 27

26th May 2018, 10:23 AM
Dolan
Dolan - avatar
4 Answers
+ 6
The above code can also be written like so: b = (lambda x: x+2)(1) a = (lambda x: x**3)(b) ----------------------- Think of the first line like this: def func(x): return x + 2 b = func(1) Here, we are passing an argument of 1 to 'func', which returns the value of 1 +2, which means b is now equal to 3. ----------------------- In the same way, the second line can also be written like this: def func2(x): return x**3 a = func2(b) Just like the first line, we are passing the value of b to func2, which returns b**3, that is 3**3 which finally results in 27. Let me know if I can clear anything up :)
26th May 2018, 10:52 AM
Just A Rather Ridiculously Long Username
+ 2
that's not an issue, lambda x: x**3 took ((lambda x: x+2)(1)) as it's argument, these 3 steps should explain: a = (lambda x: x**3)((lambda x: x+2)(1)) a = (lamda x: x**3)(3) a = 27
26th May 2018, 4:38 PM
Seb TheS
Seb TheS - avatar
+ 1
honestly a lot of the problem is we are missing the original programmers notes in code on the screen. if the programmer codes correctly notating their work a large amount of confusion would be taken out... seems like WET work.
7th Jun 2018, 4:06 AM
Ever Drakonis
27th May 2018, 12:00 PM
Dolan
Dolan - avatar