import datetime, time
def logger(fn):
def warp(*args, **kwargs):
# befor 功能增强
print('args={}, kwargs={}'.format(args, kwargs))
start = datetime.datetime.now()
ret = fn(*args, **kwargs)
# after 功能增强
duration = datetime.datetime.now() - start
print("function {} took {}s.".format(fn.__name__, duration.total_seconds()))
return ret
return warp
@ logger
def add(x, y):
print("=== call add ===")
time.sleep(2)
return x + x
print(add(4, y=7))