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))
2 Respostas
+ 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
------------
+ 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)