+ 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
3 Answers
+ 5
do_twice(add,5,10)
return add(add(5,10),add(5,10)
return add(15,15)
return 30
#outputs 30
+ 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.
+ 1
Thank you every one i understand