【发布时间】:2017-03-17 02:56:52
【问题描述】:
以下有用的装饰器测量函数的执行时间。它还打印函数的名称。但是,如果函数是方法,它还可以打印类名,那就太好了。什么是获取完整方法名称的简洁方法,例如 Class.method?
import time
def timeit(f):
def timed(*args, **kw):
ts = time.time()
result = f(*args, **kw)
te = time.time()
print('func:%r args:[%r, %r] took: %2.4f sec' % (f.__name__, args, kw, te-ts))
return result
return timed
【问题讨论】:
标签: python python-3.x decorator introspection