装饰器2

import time#引入模块
FLAG = True#用来判定是否需要用来执行装饰器里面的timmer函数
def timmer_out(FLAG):#外层判定函数
    def timmer(func):
        def inner(*args,**kwargs):
            if FLAG:#true将执行timmer里面的操作
                start = time.time()
                ret = func(*args,**kwargs)
                end = time.time()
                print(end - start)
                return ret
            else:
                ret = func(*args, **kwargs)
                return ret
        return inner
    return timmer

@timmer_out(FLAG)#调用函数timmer_out()返回timmer   @timmer语法糖实质是qqxing = timmer(qqxing)
def qqxing():
    time.sleep(0.01)
    print('taibai')

@timmer_out(FLAG)#调用函数timmer_out()返回timmer
def erguotou():
    time.sleep(0.01)
    print('alex')

qqxing()
erguotou()

 

posted @ 2019-08-31 11:38  刘华强等待瓜熟的日子  阅读(112)  评论(0编辑  收藏  举报