0

Func() inside a Func()

#Could you check my explanation def apply_twice(func, arg): return func(func(arg)) #for now ignore the above part #in the def you can write whatever you want def add_five(x): return x + 5 #in here you have a function and a variable x #x=10 from the last line, so add five x would = to 10 + 5 = 15 print(apply_twice(add_five, 10)) #now we have the first function that we ignored. #the value of x is 15 and then you going to execute the last line, which will be the last value of x which 15 + 5 = 20

20th Jun 2018, 1:18 AM
Medo Hamdani
Medo Hamdani - avatar
2 ответов
+ 3
Personally, I'd go more along the lines of: print(apply_twice(add_five, 10)) substituting apply_twice yields: print(add_five(add_five(10))) replacing the inner add_five: print(add_five(10+5)) and the outer: print(15+5) finishes with: print(20) Your ignore the above part skips how you got to add_five in the first place.
20th Jun 2018, 1:55 AM
John Wells
John Wells - avatar
0
because this part only shows def apply_twice(func, arg): return func(func(arg)) that a func must be processed within another func right
20th Jun 2018, 2:03 AM
Medo Hamdani
Medo Hamdani - avatar