0

please explain the python code.

hello. please explain the code def add(x, y): return x + y def do_twice(func, x, y): return func(func(x, y), func(x, y)) a = 5 b = 10 print(do_twice(add, a, b)) I don't understand the lines 4 and 5. thank you. link: https://code.sololearn.com/339/#py

28th Oct 2017, 5:18 PM
Dariush Shiri
Dariush Shiri - avatar
2 ответов
+ 9
Lines 4 and 5 are the definition of a method do_twice(). This method takes as arguments: a function and two additional parameters x and y. So whenever you call the do_twice method, it executes the function passed as its first attribute and passes x and y to that function -- and does this twice. This is why, when x and y (or a and b) are passed as 5 and 10, while add() is passed as the function to be executed, calling the do_twice() method does and returns the following equation: add(add(5, 10), add(5, 10)) --> add(15, 15) --> 30 Sounds easy? :)
28th Oct 2017, 6:05 PM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
+ 2
easy? not really but I am a bad ass. I will learn. Thank you for clarification
28th Oct 2017, 6:28 PM
Dariush Shiri
Dariush Shiri - avatar