码迷,mamicode.com
首页 > 其他好文 > 详细

装饰器

时间:2017-10-26 11:53:20      阅读:173      评论:0      收藏:0      [点我收藏+]

标签:深圳   功能   高阶函数   time()   func   print   运维   star   turn   

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(草创)

装饰器

标签:深圳   功能   高阶函数   time()   func   print   运维   star   turn   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!