+ 4
Please, could somebody explain this code step by step?
Please, could somebody explain this code step by step? This really seems impossible to understand to me. Although I have no problem with function with 2 arguments, seems impossible understand function with 3 argunemts. def add(x,y): return (x+y) >>>>> this code il clear and simple to understand, return first value added to second one. def do_twice(func,x,y): return func(func(x,y),func(x,y)) >>>>> this is arabic! >>>>> func(x,y) works like add(x,y)? why? >>>>> which is the real value of func? why if I remove it after "return" command , outputs are 15,15? >>>>> which is the meaning of comma between first and secondo func(x,y)? a=5 b=10 print (do_twice(add,a,b)) Please Help!
2 Réponses
+ 9
def add(x, y):
return x + y
#add(x,y) => add(a, b) => add(5,10)
#return 5 + 10 = 15
def do_twice(func, x, y):
return func(func(x, y), func(x, y))
#do_twice (func, x, y) =>
#do_twice (add, a, b) =>
#return add(add(a,b), add(a, b))=>
#return add(add(5,10), add(5,10))
#return add(15, 15)
#return 15 + 15 = 30
""" the print(do_twice(add, a, b)) changes func to add, x to a,and y to b.
At this point do_twice returns the first function add(x,y) twice, then finally solves a third add(x,y) by adding 15 + 15. """
a = 5
b = 10
print(do_twice(add, a, b))
- 1
:3