• Python自动化开发装饰器


    Python自动化开发-装饰器

    1.装饰器基础

    装饰器组成=高阶函数+函数嵌套+闭包

    装饰器:装饰器本质上就是函数,功能是为其它函数添加附加功能。

    装饰器基本原则:1)不修改被修饰函数的源代码;2)不修改被修饰函数的调用方式。

    装饰器例1:

    2.高阶函数

    高阶函数定义:如果函数接收的参数是一个函数名或者函数的返回值是一个函数名,满足这两个条件任意一个,都可称之为高阶函数。

    高阶函数例1:函数接收的参数是一个函数名

    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    
    #高阶函数例子,满足条件是函数接收的参数是函数名
    def test02():
        print("你好,北京!")
    
    def test01(test03):
        print(test03)
        test03()
    
    test01(test02)
    View Code

    代码执行结果:

    <function test02 at 0x0000020E0F217F28>
    你好,北京!

    高阶函数例2:高阶函数接收的参数是一个函数名,实现功能是统计函数执行时间

    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    
    import time
    #高阶函数例子,满足条件是函数接收的参数是函数名
    def test02():
        time.sleep(2)
        print("你好,北京!")
    
    def test01(test03):
        start_time=time.time()
        test03()
        stop_time=time.time()
        print("函数运行时间为%s" %(stop_time-start_time))
    
    test01(test02)
    View Code

    代码执行结果:

    你好,北京!
    函数运行时间为2.000218629837036

    3.装饰器的使用

    1)装饰器基本使用案例

    装饰器例1:装饰器举例,计算test函数运行时间

    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    
    import time
    #装饰器举例,计算test函数运行时间
    def test2(func):
        def test3():
            start_time=time.time()
            func()
            stop_time=time.time()
            print("test函数的运行时间为%s" %(stop_time-start_time))
        return test3
    
    #语法糖@test2 <==> test=test2(test)
    @test2
    def test():
        time.sleep(2)
        print("test函数运行结束")
    
    test()
    View Code

    代码运行结果:

    test函数运行结束
    test函数的运行时间为2.0000033378601074

    2)装饰器-函数闭包带返回值使用案例

    装饰器有关函数闭包带返回值例1:

    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    
    import time
    #装饰器举例,计算test函数运行时间
    def test2(func):
        def test3():
            start_time=time.time()
            res=func()
            stop_time=time.time()
            print("test函数的运行时间为%s" %(stop_time-start_time))
            #装饰器函数闭包带返回值
            return '这是test函数(闭包)返回值'
        return test3
    
    #语法糖@test2 <==> test=test2(test)
    @test2
    def test():
        time.sleep(4)
        print("test函数运行结束")
    res=test()
    print(res)
    View Code

    代码运行结果:

    test函数运行结束
    test函数的运行时间为4.000250339508057
    这是test函数(闭包)返回值 

    3)装饰器-函数闭包带上参数(即被修饰函数带参数)

    装饰器-被修饰函数带参数例1:

    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    
    import time
    #装饰器举例,计算test函数运行时间
    def test2(func):
        def test3(name,age):
            start_time=time.time()
            res=func(name,age)
            stop_time=time.time()
            print("test函数的运行时间为%s" %(stop_time-start_time))
            #装饰器函数闭包带返回值
            return '这是test函数(闭包)返回值'
        return test3
    
    #语法糖@test2 <==> test=test2(test)
    #被修饰函数带参数(name ,age)
    @test2
    def test(name,age):
        time.sleep(4)
        print("test函数运行结束,名字是:【%s】年龄是:【%s】" %(name,age))
    res=test("lucy",29)
    print(res)
    View Code

    代码运行结果:

    test函数运行结束,名字是:【lucy】年龄是:【29】
    test函数的运行时间为4.000201225280762
    这是test函数(闭包)返回值

    装饰器-被修饰函数带参数例2:

    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    
    import time
    #装饰器举例,计算test函数运行时间
    def test2(func):
        def test3(*args,**kwargs):
            start_time=time.time()
            res=func(*args,**kwargs)
            stop_time=time.time()
            print("test1函数的运行时间为%s" %(stop_time-start_time))
            #装饰器函数闭包带返回值
            return '这是test1函数(闭包)返回值'
        return test3
    
    #语法糖@test2 <==> test=test2(test)
    #被修饰函数带参数(name ,age,male)
    @test2
    def test1(name,age,male):
        time.sleep(4)
        print("test1函数运行结束,名字是:【%s】年龄是:【%s】 性别:【%s】" %(name,age,male))
    res=test1("lucy",29,"")
    print(res)
    View Code

    代码运行结果:

    test1函数运行结束,名字是:【lucy】年龄是:【29】 性别:【女】
    test1函数的运行时间为4.000086307525635
    这是test1函数(闭包)返回值

    你不向我走来,我便向你走去。
  • 相关阅读:
    MVC4数据访问EF查询linq语句的时候报错找不到表名问题
    以下各节已定义,但尚未为布局页“~/Views/Shared/_Layout.cshtml”呈现:“Scripts”。
    将函数实现放在头文件中
    const 不兼容的类型限定符问题
    Ubuntu 16.04重装后grub rescue> 终端模式修复方法
    Ubuntu 16.04 编译VTK7.1
    区域生长算法的一种C++实现
    Win7 U盘安装Ubuntu16.04 双系统
    Win7、Ubuntu双系统卸载Ubuntu系统
    AES加密补位填充的一个问题
  • 原文地址:https://www.cnblogs.com/renyongbin/p/15771758.html
Copyright © 2020-2023  润新知