+3

Please can someone kindly explain how the value of x was gotten

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

11/14/2019 6:07:24 AM

Kingsley Mbah

2 Answers

New Answer

+1

You can do "substitution" in your head to see it: What does `apply_twice(add_five, 10)` mean? Rename the parameters to see: def apply_twice(add_five, 10): return add_five(add_five(10)) What does `add_five(10)` mean? Rename again: def add_five(10): return 10+5 Now we can substitute into the function above: def apply_twice(add_five, 10): return add_five(10+5) Which of course is def apply_twice(add_five, 10): return add_five(15) If we substitute again that becomes def apply_twice(add_five, 10): return 20

+6

(10 + 5) + 5 = 20 The first inner call of add_five returns 10+5 = 15, the second outer call of add_five returns 15+5 = 20.