0

Plz explain me the below code ...plz....especially line 2 .....

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

20th Feb 2018, 2:41 PM
PRO
PRO - avatar
4 Respostas
+ 2
def apply_twice (func,arg): return func(arg) def add_five(x): return x+5 print (apply_twice(add_five,10)) here in the second line I have just replaced by func(arg)....plz reply fast....plz explain in easy words...
20th Feb 2018, 5:14 PM
PRO
PRO - avatar
+ 1
Okay, think of it like this. func = apply_twice() arg = 10 ^To better understand Line 2, just swap out func/arg with the above. EXAMPLE: return add_five(add_five(10)) ^As you can see when you swap it out, you're calling the add_five function twice. The inner function argument is calling 'add_five' with the value of 10, and then the function returns the new value (10 + 5) of 15, which calls the function again with the new arg of 15. The function adds 5 again resulting in a value of 20.
20th Feb 2018, 2:56 PM
Fata1 Err0r
Fata1 Err0r - avatar
0
Ever want to chat with someone on sololearn but don’t want to spam someone’s post or download discord? Try this: https://code.sololearn.com/c68t8ay789pm/?ref=app
20th Feb 2018, 3:01 PM
Jax
Jax - avatar
0
func(func(arg)) but what does this actually mean or do...
20th Feb 2018, 3:53 PM
PRO
PRO - avatar