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函数(闭包)返回值

你不向我走来,我便向你走去。
原文地址:https://www.cnblogs.com/renyongbin/p/15771758.html