本篇博客写于学完 金角大王Alex和海峰老师 于 老男孩2016年周末班S14期第4周03~08章节 装饰器 的相关内容,为对该部分视频的一个思考与补充。
下面举例子,代码如下:
例1,不使用语法糖(@xxx):
1 import time 2 3 def timer(func): 4 def deco(): 5 start_time = time.time() 6 func() 7 end_time = time.time() 8 print('running time:',end_time-start_time) 9 print('func的内存地址:',func) 10 print('deco的内存地址:',deco) 11 print('最后test的内存地址:',test) 12 return deco 13 14 def test(): 15 time.sleep(1) 16 print('in the test.') 17 18 print('原本test的内存地址:',test) 19 test = timer(test) 20 test() #调用test,test和deco指向内存中的同一个地址,这时是在调用deco
声明:1.被装饰的函数为test(),其功能为睡1s并输出'in the test.'。
2装饰器函数为timer(),其功能是使test()函数能额外输出其运行时间。
输出结果如下:
原本test的内存地址: <function test at 0x0000000003083268> in the test. running time: 5.694325685501099 func的内存地址: <function test at 0x0000000003083268> deco的内存地址: <function timer.<locals>.deco at 0x0000000003C84048> 最后test的内存地址: <function timer.<locals>.deco at 0x0000000003C84048>
让我们在调试模式下看看每个变量名的内存地址吧:
由图片可知,首次定义test函数时,test函数的函数体在内存中的地址为:<function test at 0x0000000003083268>,即test=<function test at 0x0000000003083268>
其后,第19行的代码test = timer(test)调用timer函数,将test的内存地址赋给func,此时func=test=<function test at 0x0000000003083268>
调用timer函数,deco函数的函数体放于内存中的<function timer.<locals>.deco at 0x0000000003C84048>,即demo=<function timer.<locals>.deco at 0x0000000003C84048>
19 test = timer(test) 调用timer函数,timer函数最后 12 return deco返回deco给test,此时test=demo=<function timer.<locals>.deco at 0x0000000003C84048>,即函数名(变量)
test与deco指向同一处内存地址,test函数已经不指向原来的内存地址了,test函数已经不是原来的test函数,偷梁换柱完成。
为了有更好的理解,可以自己调试看看,嘻嘻嘻。
例2,使用语法糖:
1 import time 2 3 def timer(func): 4 # print('原本test的内存地址:', test) 5 def deco(): 6 start_time = time.time() 7 func() 8 end_time = time.time() 9 print('running time:',start_time-end_time) 10 print('func的内存地址:', func) 11 print('deco的内存地址:', deco) 12 print('最后test的内存地址:', test) 13 return deco 14 15 @timer 16 def test(): 17 time.sleep(1) 18 print('in the test.') 19 20 test()
输出结果如下:
1 in the test. 2 running time: 7.125407457351685 3 func的内存地址: <function test at 0x0000000003095EA0> 4 deco的内存地址: <function timer.<locals>.deco at 0x0000000003C63048> 5 最后test的内存地址: <function timer.<locals>.deco at 0x0000000003C63048>
运行到语法糖@timer(@timer即为test=timer(test))后,test函数的函数体被放于内存<function test at 0x0000000003095EA0>中,并传给形参func,
此时test=func=<function test at 0x0000000003095EA0>,但不知道为何无法打印出test原本的内存地址 = =
将deco函数体放于内存<function timer.<locals>.deco at 0x0000000003C63048>中,即deco=<function timer.<locals>.deco at 0x0000000003C63048>,
timer函数最后将deco返回,即test=deco=<function timer.<locals>.deco at 0x0000000003C63048>,偷梁换柱完成。
可能不够清楚(我也很绝望呀),多调试几次应该就能懂了,加油哦!