please explain the code given in description.. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

please explain the code given in description..

def apply_twice(func, arg): return func(func(arg)) def add_five(x): return x + 5 print(apply_twice(add_five, 10))

19th Nov 2018, 8:05 AM
Priyanshu Ghatiya
Priyanshu Ghatiya - avatar
5 Answers
+ 2
apply_twice call recursively 2 times the add_five function... The first time, it have like param 10 get as result 15, then this value is passed as param for 2nd execution that return 20 that its returned from apply_twice call
19th Nov 2018, 8:17 AM
KrOW
KrOW - avatar
+ 2
Priyanshu Ghatiya, it is better for your progress to do this yourself in your head or on a piece of paper! The program starts with the last line. And the content of parentheses is executed from the innermost set going outwards.
19th Nov 2018, 8:46 AM
HonFu
HonFu - avatar
0
can you explain me more deeply i does not get it.Please explain the code line by line
19th Nov 2018, 8:26 AM
Priyanshu Ghatiya
Priyanshu Ghatiya - avatar
0
First two are both functions that won't execute unless called In the last line the code is trying to print the output that it receives from apply twice function so it is called and will now execute and it takes two arguments add five and 10 So apply twice calls the function once that is the add_five(10) which returns 15 . Next time add_five is called Return add_five(15) which returns output to be printed that is 20
19th Nov 2018, 8:06 PM
Ash
0
Now that was what the requester was looking for. Well done! Thanks for taking the time to explain the code line-for-line.
17th Oct 2019, 7:15 AM
William