- 1

Please any one of you please the below code to me

def apply_twice(func, arg): return func(func(arg)) def add_five(x): return x + 5 print(apply_twice(add_five, 5)) Output is 15

1st Sep 2022, 9:22 AM
S GOUSE BASHA
S GOUSE BASHA - avatar
5 Respostas
+ 6
let me try to break it down for you. 1. the apply_twice function has two arguments. The first Argument will hold another function. then the second function will hold the number. 2. The return statement in the apply_twice function will call the add_five function twice and pass in a given argument which is a number. 3. The add_five function is defined and it returns the mathematical operation 4. In The print statement it will output the result. In The print statement the apply_twice function is called with the add_five function and a number. in the return statement of the apply_twice function. add_five function is called twice. let me give how the return statement will work return func(arg) # it will be 5+5 => 10 return func(func(arg)) #it will be 10+5 => 15
1st Sep 2022, 10:30 AM
MATOVU CALEB
MATOVU CALEB - avatar
+ 4
MATOVU CALEB Nicely explained 😁👍
1st Sep 2022, 10:43 AM
Rik Wittkopp
Rik Wittkopp - avatar
+ 3
Hi! do you not understand how the result was 15?
1st Sep 2022, 9:47 AM
Yaroslav Vernigora
Yaroslav Vernigora - avatar
+ 2
add_five() wrapped into apply_twice() unrolls into: add_five(add_five(5)) because it follows the pattern: func(func(arg)) func is add_five arg is 5
3rd Sep 2022, 12:52 AM
Patrick
Patrick - avatar
+ 1
Yeah I didn't understand, how the output is 15, would you please explain me
1st Sep 2022, 10:28 AM
S GOUSE BASHA
S GOUSE BASHA - avatar