• Python随心记--装饰器


    装饰器
    本质就是函数,功能是为其他函数添加附加功能
    原则是:不修改被修饰函数的源代码
    不修改被修饰函数的调用方式
    统计函数的运行时间 
    import time
    def cal(l):
        start_time = time.time()
        res = 0
        for i in l:
            time.sleep(0.1)
            res += i
        stop_time = time.time()
        print('函数的运行时间是%s秒' %(stop_time - start_time))
        return res
    print(cal(range(10)))
    加上装饰器方法 统计函数的运行时间
    import time
    def timmer(func):
        def wapper(*args,**kwargs):
            start_time = time.time()
            res = func(*args,**kwargs)
            stop_time = time.time()
            print('函数的运行时间是%s秒' %(stop_time - start_time))
            return res
        return wapper
    @timmer
    def cal(l):
        res = 0
        for i in l:
            time.sleep(0.1)
            res += i
        return res
    print(cal(range(10)))
    装饰器的知识储备
    装饰器 = 高阶函数+函数嵌套+闭包
    高阶函数
    函数接收的参数是一个函数名
    函数的返回值是一个函数名
    满足上述任意条件一个都是高阶函数
    
    
    def foo():
        print('你好啊林师傅')
    def test(func):   #高阶函数
        print(func)
        func()
    print(test(foo))
    
    
    import time
    def foo():
        print('你好啊林师傅')
    def test(func):
        print(func)
        start_time = time.time()
        func()
        stop_time = time.time()
        print('函数的运行时间是%s秒' % (stop_time - start_time))
    print(test(foo))
    注:以上多运行了一次foo函数  

    #高阶函数满足不了装饰器
    
    
    import time
    def foo():
        time.sleep(1)
        print('来自foo')
    #不修改foo代码
    #不修改foo调用方式
    def timer(func):
        start_time = time.time()
        func()
        stop_time = time.time()
        print('函数的运行时间是%s秒' % (stop_time - start_time))
        return func
    foo = timer(foo)
    print(foo())
    
    
    函数嵌套:函数里面定义一个函数
    def father(name):
        print('from father %s' %name)
        def son():
            print('from son')
            def grandson():
                print('from grandson')
            grandson()
        son()
    father('大佬')
    def father(name):
        print('from father %s' %name)
        def son():
            print('from son')
        print(locals())
    father('大佬')
    函数嵌套及作用域
    def father(name):
        def son():
            print('from son %s' %name)
        son()
    father('大佬')
    def father(name):
        def son():
            name = '小豆芽'
            print('from son %s' %name)
        son()
    father('大佬')

    函数闭包装饰器基本实现
    装饰器的框架
    import time
    def itmmer(func):
        def wrapper():
            strat_time = time.time()
            func()   #执行test函数
            stop_time = time.time()
            print('函数的运行时间是%s秒' % (stop_time - strat_time))
        return wrapper
    def test():
        time.sleep(3)
        print('test函数运行完毕')
    test = itmmer(test)#返回的是wrapper的地址
    test()
    #   @timmer  相当于 test = itmmer(test)  语法糖
    import time
    def itmmer(func):
        def wrapper():
            strat_time = time.time()
            func()   #执行test函数
            stop_time = time.time()
            print('函数的运行时间是%s秒' % (stop_time - strat_time))
        return wrapper
    @itmmer
    def test():
        time.sleep(3)
        print('test函数运行完毕')
    test()
    这里添加test返回值,,解决函数最后返回的None
    import time
    def itmmer(func):
        def wrapper():
            strat_time = time.time()
            res = func()   #执行test函数
            stop_time = time.time()
            print('函数的运行时间是%s秒' % (stop_time - strat_time))
            return res
        return wrapper
    @itmmer
    def test():
        time.sleep(1)
        print('test函数运行完毕')
        return '这里添加test返回值,,解决函数最后返回的None'
    res = test()   #这里会最后会返回一个None
    print(res)

    带参数
    import time
    def itmmer(func):
        def wrapper(name,age):
            strat_time = time.time()
            res = func(name,age)   #执行test函数
            stop_time = time.time()
            print('函数的运行时间是%s秒' % (stop_time - strat_time))
            return res
        return wrapper
    @itmmer
    def test(name,age):
        time.sleep(1)
        print('test函数运行完毕,名字是%s,年龄是%s' %(name,age))
        return '这里添加test返回值,,解决函数最后返回的None'
    test('ss',20)
    带参数 修改为可变参数
    import time
    def itmmer(func):
        def wrapper(*args,**kwargs):
            strat_time = time.time()
            res = func(*args,**kwargs)   #执行test函数
            stop_time = time.time()
            print('函数的运行时间是%s秒' % (stop_time - strat_time))
            return res
        return wrapper
    @itmmer
    def test(name,age):
        time.sleep(1)
        print('test函数运行完毕,名字是【%s】,年龄是【%s】' %(name,age))
        return '这里添加test返回值,,解决函数最后返回的None'
    test('ss',20)
    可变参数 *args,**kwargs 传参实例
    def test2(name,age,gender):
        print(name)
        print(age)
        print(gender)
    def test1(*args,**kwargs):
        test2(*args,**kwargs)   #test2(*('alex',18,'male','x','y'),**{})
    
    
    
    
  • 相关阅读:
    sudo在shell脚本执行的问题
    mahout的数据文件格式
    mahout概述
    基于keepalived的redis系统master双机热备,读数据负载均衡设置方案
    输入挂(读数大致格式)
    POJ3762 The Bonus Salary! (费用流+离散化)
    codeforces round 321 div2 D Kefa and Dishes(状态压缩dp)
    HDU5492 Find a path (dp)
    大数模版
    RMQ模版
  • 原文地址:https://www.cnblogs.com/Essaycode/p/10126567.html
Copyright © 2020-2023  润新知