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)
    • 注意:装饰器不能直接用于迭代函数,否则会把装饰器一起迭代,需要使用另一个添加装饰器的函数调用需要装饰的函数
  • 相关阅读:
    javascript的window.open()具体解释
    Async.js解决Node.js操作MySQL的回调大坑
    入门--JTBC系统学习(1)
    Hadoop1.2.1 全然分布式集群搭建实操笔记
    Hessian原理与程序设计
    pycharm最新注册方法 pycharm最新激活方法 2016pycharm最新注册方法
    如何修改linux时间? 校正linux系统的时间
    python 内建函数 type() 和 isinstance() 介绍
    标准类型内建函数 str()和 repr() (及 `` 运算符) 简单介绍
    标准类型内建函数 cmp()介绍
  • 原文地址:https://www.cnblogs.com/dbf-/p/10575097.html
Copyright © 2011-2022 走看看