前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Python装饰器原理——偷梁换柱

Python装饰器原理——偷梁换柱

作者头像
py3study
发布2020-01-15 16:30:30
3240
发布2020-01-15 16:30:30
举报
文章被收录于专栏:python3

本篇博客写于学完  金角大王Alex和海峰老师  于  老男孩2016年周末班S14期第4周03~08章节   装饰器  的相关内容,为对该部分视频的一个思考与补充。

下面举例子,代码如下:

例1,不使用语法糖(@xxx):

代码语言:javascript
复制
 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()函数能额外输出其运行时间。

输出结果如下:

代码语言:javascript
复制
原本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,使用语法糖:

代码语言:javascript
复制
 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()

输出结果如下:

代码语言:javascript
复制
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>,偷梁换柱完成。

可能不够清楚(我也很绝望呀),多调试几次应该就能懂了,加油哦!

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019/06/08 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档