0

Code Function as object

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)) explain<<do_twice>>>

25th Feb 2018, 7:38 AM
Jordan
Jordan  - avatar
1 Odpowiedź
+ 4
So everything here starts with 'print(do_twice(add, a, b))' And then do_twice replaces variables in return statement and it looks like this 'add(add(a, b), add(a, b))' After running both inside add(a, b) funcs this outside func becomes add(15, 15) So then do_twice func returns 30, which is then the result Hope this is a good explanation(you also have some explanations in comment section of the lesson)
25th Feb 2018, 10:07 AM
Tim Thuma
Tim Thuma - avatar