Discuss / Python / Answers from Maplin on 2022-11-30

Answers from Maplin on 2022-11-30

Topic source

第一题:

>>> 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

...


  • 1

Reply