zoukankan      html  css  js  c++  java
  • Python装饰器

    无返回值的装饰器:

    import time
    
    
    def timer(func):                         # 无返回值装饰器
        def test_extra(*args, **kwargs):     # 传入参数长度可变(参数多少都可用)
            start_time = time.time()         # 装饰器添加功能:计时
            func(*args, **kwargs)            # 执行原函数功能
            end_time = time.time()
            print("The func has run for %s s
    " % (end_time - start_time))
        return test_extra                    # 返回的是test_extra的内存地址,不是test_extra的运行结果
    
    
    @timer  # text1 = timer(text1)
    def test():                              # 原函数不需要参数
        time.sleep(1)
        print("This is test1.")
    
    test()                                   # 调用方式不变

    输出结果:

    This is test1.
    The func has run for 1.000288248062134 s

    def timer(func):                         # 无返回值装饰器
        def test_extra(*args, **kwargs):     # 传入参数长度可变(参数多少都可用)
            start_time = time.time()         # 装饰器添加功能:计时
            func(*args, **kwargs)            # 执行原函数功能
            end_time = time.time()
            print("The func has run for %s s
    " % (end_time - start_time))
        return test_extra                    # 返回的是test_extra的内存地址,不是test_extra的运行结果
    
    
    @timer
    def test(name, age):                     # 原函数需要参数
        print("In the test2")
        time.sleep(2)
        print("Your name is " + name + age)
    
    test("name", "1")                        # 调用方式不变

    输出结果:

    In the test2
    Your name isname1
    The func has run for 2.0002686977386475 s

    有返回值的装饰器:

    import time
    
    
    def timer(func):                         # 有返回值的装饰器
        def test_extra(*args, **kwargs):     # 传入参数长度可变(参数多少都可用)
            start_time = time.time()         # 装饰器添加功能:计时
            res = func(*args, **kwargs)      # 获得func函数的返回值
            end_time = time.time()
            print("The func has run for %s s
    " % (end_time - start_time))
            return res                       # 返回原函数的返回值
        return test_extra                    # 返回的是test_extra的内存地址,不是test_extra的运行结果
    
    
    @timer
    def test(content):
        print("In the test")
        time.sleep(2)
        print(content)
        return "This is return of test"
    
    print(test("666"))                        # 打印出test的返回值

    输出结果:

    In the test
    666
    The func has run for 2.0005550384521484 s

    This is return of test

    和之前的区别只是最内层的函数有了返回值,等于原函数的返回值

    可以传给装饰器传一个额外参数的装饰器

    import time
    
    
    def timer(parameter):                    # 可以传给装饰器传一个额外的参数的装饰器
        def timer_in(func):
            def test_extra(*args, **kwargs): # 传入参数长度可变(参数多少都可用)
                print(parameter)
                start_time = time.time()
                func(*args, **kwargs)        # 获得func函数的返回值
                end_time = time.time()
                print("The func has run for %s s
    " % (end_time - start_time))
            return test_extra                # 返回的是test_extra的内存地址,不是test_extra的运行结果
        return timer_in
    
    
    @timer(parameter="This is an extra parameter")        # 给装饰器传入一个参数
    def test(content):
        time.sleep(2)
        print("This is in the test")
        print(content)
    
    test("666")                              # 打印出test的返回值

    输出结果:

    This is an extra parameter
    This is in the test4
    666
    The func has run for 2.0006258487701416 s

    和之前的区别是又多加了一层函数

    小结

    • 装饰器就是先嵌套一层函数添加功能,再嵌套一层函数,这层函数的返回值是里面那一层函数的内存地址
    • 装饰器中函数的返回值不要加括号,传入的是内存地址,不是运行结果
    • 装饰器给原函数传参用(*args, **kwargs)
    • 注意:装饰器不能直接用于迭代函数,否则会把装饰器一起迭代,需要使用另一个添加装饰器的函数调用需要装饰的函数
  • 相关阅读:
    《ACM国际大学生程序设计竞赛题解I》——6.8
    数据结构篇
    从SG函数浅谈解决博弈问题的通法
    动态规划的泛式解题思路
    bzoj1057: [ZJOI2007]棋盘制作
    bzoj3884: 上帝与集合的正确用法
    bzoj1564: [NOI2009]二叉查找树
    bzoj4347: [POI2016]Nim z utrudnieniem
    bzoj1131: [POI2008]Sta
    bzoj1566: [NOI2009]管道取珠
  • 原文地址:https://www.cnblogs.com/dbf-/p/10575097.html
Copyright © 2011-2022 走看看