标签:basic int pre run outer asi import 第一步 val
#基本代码:
import time
def decoratorrunlog_args(logval):
print("logval is runing"+str(logval))
def decoratorrunlog(func):
print("outerlog is runing")
def inner(*args,**kwargs):
print("innerlog is runing")
stime = time.time()
time.sleep(1)
res = func(*args,**kwargs)
etime = time.time()
print(etime - stime)
print("innerlog is stoping")
return res
print("outerlog is stoping")
return inner
print("logval is stoping")
return decoratorrunlog
def decoratortuntime_args(timeval):
print("timeval is runing:"+str(timeval))
def decoratorruntime(func):
print("outer is runing")
def inner(*args,**kwargs):
print("inner is runing")
stime = time.time()
time.sleep(1)
res = func(*args,**kwargs)
etime = time.time()
print(etime - stime)
print("inner is stoping")
return res
print("outer is stoping")
return inner
print("timeval is stoping")
return decoratorruntime
@decoratorrunlog_args("argsonelog")
@decoratortuntime_args("argstwotime")
def funcone(pname):
print("this is a basic function")
return pname
res = funcone("lily")
print(res)
#执行结果
logval is runingargsonelog
logval is stoping
timeval is runing:argstwotime
timeval is stoping
outer is runing
outer is stoping
outerlog is runing
outerlog is stoping
innerlog is runing
inner is runing
this is a basic function
1.000281810760498
inner is stoping
2.000582218170166
innerlog is stoping
lily
#图示
#说明:
图中:1.1,1.2,2.1,2.2 中, "."之前的数字表示执行的步骤,之后的数字表示多个装饰器的数字标识;
#运行顺序
基本是按照代码编写顺序执行
第一步:1.1、1.2: 执行带参数的装饰器本身,
第二步:2.2、2.1:执行装饰器
第三步:3.1、3.2:执行装饰器内部的闭包函数
第四步:func:执行装饰器装饰的函数本身
第五步:5.2、5.1:执行闭包中func后面的部分
装饰器执行的顺序:按照其在所装饰的函数中放置的顺序来执行。首先从最上面的装饰器开始按顺序执行到函数位置,然后在从函数位置向上执行到最上面的装饰器,再接着向下执行,如此反复直到装饰器最内部的函数执行到被装饰的函数本身之后才开始执行被装饰的函数。顺序类似于:↓↑↓↑
标签:basic int pre run outer asi import 第一步 val
原文地址:https://blog.51cto.com/11089980/2355409