import functools
def log(text=None):
def decorator(func):
@functools.wraps(func)
def wrapper(*args, **kw):
if not text is None:
print(text)
print('begin call')
result=func(*args, **kw)
print('end call')
return result
return wrapper
return decorator
@log('hello')
def now(x,y):
print(x+y)
return x+y
print(now(1,2))
我想问一下,有没有什么办法不在原函数中添加print(x + y)就可以实现在出结果后才打印endcall呢
你这样不能@log这样写
Sign in to make a reply
coLBooy
import functools
def log(text=None):
def decorator(func):
@functools.wraps(func)
def wrapper(*args, **kw):
if not text is None:
print(text)
print('begin call')
result=func(*args, **kw)
print('end call')
return result
return wrapper
return decorator
@log('hello')
def now(x,y):
print(x+y)
return x+y
print(now(1,2))