第一题:
>>> def metric(fn):
... @functools.wraps(fn)
... def wrapper(*args, **kw):
... t = time.time()
... result = fn(*args, **kw)
... print('%s executed in %s ms' % (fn.__name__, round((time.time() - t), 3)))
... return result
... return wrapper
...
第二题:
>>> def log(*text):
... def decorator(func):
... @functools.wraps(func)
... print('begin call')
... result = func(*args, **kw)
... print('end call')
... return decorator
>>> @log('execute')
... def f():
... pass
Sign in to make a reply
Maplin (^ω^)
第一题:
>>> def metric(fn):
... @functools.wraps(fn)
... def wrapper(*args, **kw):
... t = time.time()
... result = fn(*args, **kw)
... print('%s executed in %s ms' % (fn.__name__, round((time.time() - t), 3)))
... return result
... return wrapper
...
第二题:
>>> def log(*text):
... def decorator(func):
... @functools.wraps(func)
... def wrapper(*args, **kw):
... print('begin call')
... result = func(*args, **kw)
... print('end call')
... return result
... return wrapper
... return decorator
...
>>> @log('execute')
... def f():
... pass
...