+ 2

Function as objects

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)) please explain deeply because these type of concept is new for me

30th Apr 2018, 5:13 AM
Lakshman Patel
Lakshman Patel - avatar
3 Respostas
+ 5
do_twice(add,5,10) return add(add(5,10),add(5,10) return add(15,15) return 30 #outputs 30
30th Apr 2018, 5:29 AM
᠌᠌Code X
᠌᠌Code X - avatar
+ 3
def add(x,y): return a+b def do_twice(func,x, y): return func(func(x,y),func(x,y)) 👆. 👆. 👆 add. add. add print(do_twice(add,5, 3)) 👆. 👆 👆 func. x. y where fun is add ,x=5, y=3,now you can think how this do_twice function works.
30th Apr 2018, 5:28 AM
Maninder $ingh
Maninder $ingh - avatar
+ 1
Thank you every one i understand
1st May 2018, 4:24 PM
Lakshman Patel
Lakshman Patel - avatar