Discuss / Python / #练习 设计一个decorator,它可作用于任何函数上,并打印该函数的执行时间

#练习 设计一个decorator,它可作用于任何函数上,并打印该函数的执行时间

Topic source

城南梦想

#1 Created at ... [Delete] [Delete and Lock User]

#--coding:utf-8 -- import functools,time def metric(txt): def decorator(func): @functools.wraps(func)

    def wrapper(*args, **kw) 
        print("%s %s %s()"%(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))    \
        ,txt,func.__name__))
        return func
    return wrapper
return decorator

@metric("调用") def noow(): print("-aaaa-")


  • 1

Reply