How can I decorate a function with "sum" (I don't want just print something, but doing a "add" function) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How can I decorate a function with "sum" (I don't want just print something, but doing a "add" function)

My code as bellow, and why it not works ? def deco(any_func): def wrap(): print("::::::::::::") any_func() print("------------") return wrap() def p(x,y): return x+y deco(p(6,3))

30th Mar 2019, 10:39 PM
Joe Lishuai Lin
Joe Lishuai Lin - avatar
2 Answers
+ 3
This is one of many solutions to your problem. def deco(any_func,x,y): def wrap(): print("::::::::::::") print(any_func(x,y)) print("------------") return wrap() def p(x,y): return x+y deco(p,6,3) Output: :::::::::::: 9 ------------
30th Mar 2019, 11:00 PM
Diego
Diego - avatar
+ 3
This is another way of doing it. def deco(any_func): def wrap(x,y): print("::::::::::::") print(any_func(x,y)) print("------------") return wrap def p(x,y): return x+y deco(p)(6,3)
30th Mar 2019, 11:06 PM
Diego
Diego - avatar