第34天python学习装饰器:函数嵌套

#函数嵌套:在函数里面定义一个函数
# def father(name):
# print("form father %s"%name)
# def son():
# print("form son")
# def grandson():
# print("from grandson")
# grandson()
# son()
# father("zhangshan")

#函数嵌套升级成包:闭包和函数作用域一样
# def father(name):
# print("form father %s"%name)
# def son():
# print("form son")
# def grandson():
# name="zhangda"
# print("from grandson %s" %name)
# grandson()
# son()
# father("zhangshan")


#装饰器的框架:满足高级函数(参数和返回值是函数)+函数嵌套(函数里面定义函数)+闭包(print(fun)的参数直接是time(fun),fun
#的的参数————作用域,外面直接传值到print)
#
# def time(fun):
# def wraper():
# print(fun)
# fun()
# return wraper

# #统计test的执行时间
import time
def timme(fun):
def wraper():
start_time=time.time()#开始时间
fun()#运行test()
stop_time=time.time()#停止时间
print("运行时间函数时间 %s" %(stop_time-start_time))
return wraper

#test被修修饰的函数
@timme#@time就相当于test=timme(test)
def test():
time.sleep(3)
print("test函数运行完毕")
# test=timme(test)#把值赋给test函数,,这行不要,因为有@timme了
test()

原文地址:https://www.cnblogs.com/jianchixuexu/p/11605375.html