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

    python装饰器就是用于拓展原来函数功能的一种函数,这个函数的特殊之处在于它的返回值也是一个函数,使用python装饰器的好处就是在不用更改原函数的代码前提下给函数增加新的功能。 

    例如,源函数代码如下: 

    import time
    def func(a,b): print("hello,here is a func for add :") time.sleep(1) print("result is %d" %(a+b))

      

    如果想记录下这个函数执行的总时间

    #带有参数的装饰器
    import time
    
    def deco(func):
        def wrapper(a,b):
            startTime = time.time()
            func(a,b)
            endTime = time.time()
            msecs = (endTime - startTime)*1000
            print("time is %d ms" %msecs)
        return wrapper
    
    
    @deco
    def func(a,b):
        print("hello,here is a func for add :")
        time.sleep(1)
        print("result is %d" %(a+b))
    
    if __name__ == '__main__':
        f = func
        f(3,4)
        #func()
    
    #带有不定参数的装饰器
    import time
    
    def deco(func):
        def wrapper(*args, **kwargs):
            startTime = time.time()
            func(*args, **kwargs)
            endTime = time.time()
            msecs = (endTime - startTime)*1000
            print("time is %d ms" %msecs)
        return wrapper
    
    
    @deco
    def func(a,b):
        print("hello,here is a func for add :")
        time.sleep(1)
        print("result is %d" %(a+b))
    
    @deco
    def func2(a,b,c):
        print("hello,here is a func for add :")
        time.sleep(1)
        print("result is %d" %(a+b+c))
    
    
    if __name__ == '__main__':
        f = func
        func2(3,4,5)
        f(3,4)
        #func()
    

     #多个装饰器

    #多个装饰器
    
    import time
    
    def deco01(func):
        def wrapper(*args, **kwargs):
            print("this is deco01")
            startTime = time.time()
            func(*args, **kwargs)
            endTime = time.time()
            msecs = (endTime - startTime)*1000
            print("time is %d ms" %msecs)
            print("deco01 end here")
        return wrapper
    
    def deco02(func):
        def wrapper(*args, **kwargs):
            print("this is deco02")
            func(*args, **kwargs)
    
            print("deco02 end here")
        return wrapper
    
    @deco01
    @deco02
    def func(a,b):
        print("hello,here is a func for add :")
        time.sleep(1)
        print("result is %d" %(a+b))
    
    
    
    if __name__ == '__main__':
        f = func
        f(3,4)
        #func()
    
    '''
    this is deco01
    this is deco02
    hello,here is a func for add :
    result is 7
    deco02 end here
    time is 1003 ms
    deco01 end here
    '''
    

    多个装饰器执行的顺序就是从第一个装饰器开始,执行到最后一个装饰器,再执行函数本身。 

    这里的deco函数就是最原始的装饰器,它的参数是一个函数,然后返回值也是一个函数。其中作为参数的这个函数func()就在返回函数wrapper()的内部执行。然后在函数func()前面加上@deco,func()函数就相当于被注入了计时功能,现在只要调用func(),它就已经变身为“新的功能更多”的函数了。 

      

  • 相关阅读:
    笔记:Oracle SQL 高级编程 第2章 SQL 执行
    python 中的 filter, lambda, map, reduce 内置函数
    笔记:Oracle SQL 高级编程 第1章 SQL 核心
    java大文件读写操作,java nio 之MappedByteBuffer,高效文件/内存映射
    使用JDBC进行批处理
    程序员面试、算法研究、编程艺术、红黑树、数据挖掘5大系列集锦
    教你如何迅速秒杀掉:99%的海量数据处理面试题
    十道海量数据处理面试题与十个方法大总结
    《Java 7 并发编程指南》学习概要 (7) 定制并发类
    HashMap多线程并发问题分析
  • 原文地址:https://www.cnblogs.com/saryli/p/9031455.html
Copyright © 2011-2022 走看看