首页 > 编程语言 > 详细

python装饰器

时间:2017-10-26 15:13:15      阅读:309      评论:0      收藏:0      [点我收藏+]

1.装饰器
1)本质就是函数,(装饰其他函数),为其他函数添加附加功能
2)原则:
1.不能修改被装饰的函数源代码
2.不能修改被装饰的函数的调用方式

实现装饰器知识储备:
1.函数即”变量“
2.高阶函数
3.嵌套函数
高阶函数+嵌套函数=装饰器

import timedef trrm(fuc): #trrm(test1)
    def cod(*args,**kwargs):
        start_time=time.time()
        fuc(*args,**kwargs) #run test1()
        stop_time=time.time()
        print(‘the fuc run is %s‘ %(stop_time-start_time))    return cod@trrmdef test1():
    time.sleep(3)
    print(‘the is test1‘)@trrmdef test2():
    time.sleep(3)
    print(‘the is test2‘)

test1()
test2()
>>>>>
the is test1
the fuc run is 3.0005998611450195the is test2
the fuc run is 3.007200002670288


本文出自 “python” 博客,请务必保留此出处http://13430974.blog.51cto.com/13420974/1976306

python装饰器

原文:http://13430974.blog.51cto.com/13420974/1976306

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!