zoukankan      html  css  js  c++  java
  • python装饰器小记

    1.装饰器:本质是函数,是用来给其他函数添加附加扩展功能的函数,其返回值是一个函数(函数指针)

    2.装饰器作用:不改变函数源代码和函数调用方式的前提下添加函数的附加功能。

    3.装饰器储备知识点:

      A.高阶函数

      B.嵌套函数(闭包函数)

      C.“函数即变量”

    4.装饰器函数的外部函数传入要装饰的函数名字,返回经过修饰后函数的名字;内层函数(闭包)负责修饰被修饰函数。

        实质: 是一个函数

        参数:是你要装饰的函数名(并非函数调用

        返回:是装饰好的函数名(也非函数调用

        作用:为已经存在的对象添加额外的功能

        特点:不需要对对象做任何的代码上的变动

    5.装饰器分类:例子

      A.一般装饰器(函数内调用另一个函数)

       

    
    
    import time
    def decorator(function): #计算出函数function运行时间
    start_time=time.time()
    function()
    stop_time=time.time()
    run_time=start_time-stop_time
    print("function runs %s"%run_time)

    def test(): #测试函数
    print('in the test')

    decorator(test) #调用装饰器

    执行结果如下:

      B.简单装饰器

    import time
    def decorator(function):  #计算出函数function运行时间
            start_time=time.time()
            function()
            stop_time=time.time()
            run_time=start_time-stop_time
            print("function runs %s"%run_time)
    @decorator  #被装饰函数定义之前加入@decorator,之后可以直接调用被装饰函数。
    def test():  #测试函数
            print('in the test')
    
    test  #调用被装饰函数
    
    执行结果如下:


    3.被装饰函数带一个参数的装饰器

    
    
    
    
    import time
    def decorator1(function): #计算出函数function运行时间
    def decorator2(x): #参数代入
    start_time=time.time()
    function(x) #参数代入,此处参数名必须与上一步参数名一样
    stop_time=time.time()
    run_time=start_time-stop_time
    print("function runs %s"%run_time)
    return decorator2 #返回decorator,此处人的理解是调用decorator1返回decorator2的引用

    @decorator1 #被装饰函数定义之前加入@decorator,之后可以直接调用被装饰函数。
    def test(args): #测试函数
    print('in the test',args)

    test("argument") #调用被装饰函数,此处实参为字符串argument

    执行结果:

    4.被装饰函数带多个或者不确定参数个数的装饰器:此处回顾函数参数调用的内容(*args,**kwargs)

    import time
    def decorator1(function): #计算出函数function运行时间
    def decorator2(*args,**kwargs): #参数代入
    start_time=time.time()
    function(*args,**kwargs) #参数代入,此处参数名必须与上一步参数名一样
    stop_time=time.time()
    run_time=start_time-stop_time
    print("function runs %s"%run_time)
    return decorator2 #返回decorator,此处本的理解是两个嵌套函数的逐个调用

    @decorator1 #被装饰函数定义之前加入@decorator,之后可以直接调用被装饰函数。
    def test(args1,args2): #测试函数
    print('in the test',args1,args2)

    test('multiple','argument') #调用被装饰函数,此处实参为字符串argument

    执行结果:

    5.自身带参数的装饰器

    
    
    import time
    def decorator1(args1): #args1作用于装饰器decorator1内部
    def decorator2(args2): #
    def decorator3(*args,**kwargs): #参数代入
    start_time=time.time()
    for i in range(args1): #args1作用于此
    args2(*args,**kwargs) #参数代入,此处参数名必须与上一步参数名一样
    stop_time=time.time()
    run_time=start_time-stop_time
    print("function runs %s"%run_time)
    return decorator3 #返回decorator3,此处人的理解是返回decorator3的引用
    return decorator2 #返回decorator2,此处人的理解是返回decorator2的引用

    @decorator1(3) #被装饰函数定义之前加入@decorator,之后可以直接调用被装饰函数。
    def test(args1,args2): #测试函数
    print('in the test',args1,args2)

    test('multiple','argument') #调用被装饰函数,此处实参为字符串argument

    执行结果:

     

    6.多个装饰器使用:

    
    
    import time
    def decorator1(x): #args1作用于装饰器decorator1内部
    def decorator2(args2): #
    def decorator3(*args,**kwargs): #参数代入
    start_time=time.time()
    for i in range(x): #args1作用于此
    args2(*args,**kwargs) #参数代入,此处参数名必须与上一步参数名一样
    stop_time=time.time()
    run_time=start_time-stop_time
    print("function runs %s"%run_time)
    return decorator3 #返回decorator3,此处人的理解是返回decorator3的引用
    return decorator2 #返回decorator2,此处人的理解是返回decorator2的引用

    def decorator4(funct):
    def decorator5(*args,**kwargs):
    print('-------------decorator4 start-------------')
    funct(*args,**kwargs)
    print('------------decoratoe4 end here----------')
    print(' ')
    return decorator5

    @decorator1(3) #此处的decorator1装饰了下面的的整体
    @decorator4 #此处的decorator4装饰了test
    def test(args1,args2): #测试函数
    print('in the test',args1,args2)

    test('multiple','argument') #调用被装饰函数,此处实参为字符串argument


    执行结果:

    注:A.使用参数时要理解每一个参数代入的量(被装饰函数还是一般参数)

      B.多个装饰器嵌套使用尤其要注意装饰的顺序和层次

    ###新手小白,请多多指正

  • 相关阅读:
    [BZOJ3295][Cqoi2011]动态逆序对 CDQ分治&树套树
    [BZOJ3230] 相似字串 后缀数组+RMQ
    [BZOJ4556][Tjoi2016&Heoi2016]字符串 后缀数组+主席树
    [BZOJ4044]Virus synthesis 回文自动机的DP
    [BZOJ2055]80人环游世界 有上下界最小费用最大流
    [BZOJ2502]清理雪道 有上下界网络流(最小流)
    [BZOJ2095][Poi2010]Bridges 最大流(混合图欧拉回路)
    [BZOJ2288&BZOJ1150]一类堆+链表+贪心问题
    [BZOJ4820]硬币游戏 KMP+高斯消元
    [BZOJ1559]密码 AC自动机+状压
  • 原文地址:https://www.cnblogs.com/wasua/p/9046929.html
Copyright © 2011-2022 走看看