装饰器1_统计时间函数装饰欢迎登录函数

import  time
def timmer(func):   #这里是要把需要装饰的函数名传递进来,方便后面调用。如果不传函数名而是直接调用,在调用的位置会报:RecursionError: maximum recursion depth exceeded while calling a Python object
    def wrapper(*args,**kwargs):
        time_start = time.time()
        res=func(*args,**kwargs)   #1、这里要使用func传递进来的形参名。2、定义成这种形式是方便,不管被装饰的函数有无参数,有无返回值,程序运行都不会报错
     time_stop = time.time() time_run = time_stop - time_start print('run time is %s' % time_run)      return res
return wrapper @timmer def log(name): time.sleep(3) print('welcome %s to zhuang' % name)

  

原文地址:https://www.cnblogs.com/wangkc/p/6916060.html