标签:返回 定义 装饰器 高阶函数 就是 添加 imm 一个 time()
装饰器:本质上就是一个函数(装饰其它函数):为其他函数添加附属功能。
原则:
实现装饰器(decorator)的知识储备:
函数即变量:
嵌套函数:在一个函数体内用def定义了一个函数
装饰器:@ 函数名
# 装饰器
import time
def timmer(func):
    #重写函数功能
    def changeFunc():
        time_start=time.time()
        func()
        time_stop=time.time()
        print("this func have use %s time" %(time_stop-time_start))
    return changeFunc
@timmer #类似于代码 test1=timmer(test1) test1()给函数重新附上新的首地址
def test():
    time.sleep(3)
    print("in the test")
test()
标签:返回 定义 装饰器 高阶函数 就是 添加 imm 一个 time()
原文地址:https://www.cnblogs.com/qkqBeer/p/9114105.html