首页 > 其他 > 详细

装饰器

时间:2017-10-26 11:05:34      阅读:245      评论:0      收藏:0      [点我收藏+]

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

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

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

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

深圳运维开发交流群 --->>599706738(草创)

装饰器

原文:http://www.cnblogs.com/huangbipeng/p/7735589.html

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