decorator.py
def print_info(func):
def wrapper(*args, **kwargs):
print('start')
result = func(*args,**kwargs)
print('end')
return result
return wrapper
#Useful when adding something to a function (be careful in order when applying multiple decorators)
@print_info
def add_num(a, b):
return a+b
r = add_num(10,20)
print(r)
#If you don't use a decorator, you can write as below, but it's hard to understand
#f = print_info(add_num)
#r = f(10,20)
#print(r)
output:
start
end
30
Recommended Posts