【Python】函数式编程 (装饰器)

本文深入探讨Python装饰器,从装饰器的本质——函数闭包开始,逐步介绍如何使用装饰器增强函数功能,包括保留参数和返回值的闭包,以及如何处理多个装饰器的执行顺序。同时,文章展示了如何创建带参数的装饰器,通过实例详细解释了参数传递和设置返回值的过程,帮助读者全面理解Python装饰器的运用。
摘要由CSDN通过智能技术生成

目录

一、装饰器的本质:

函数闭包(function closure):

二、装饰器使用方法:

保留函数参数和返回值的函数闭包:

三、多个装饰器的执行顺序:

四、创建带参数的装饰器:


一、装饰器的本质:

        装饰器(decorator)本质是函数闭包(function closure)的语法糖(Syntactic sugar)

函数闭包(function closure):

        函数闭包是函数式语言(函数是一等公民,可作为变量使用)中的术语。函数闭包:一个函数,其参数和返回值都是函数,用于增强函数功能面向切面编程(AOP)

import time


# 控制台打印100以内的奇数:
def print_odd():
    for i in range(100):
        if i % 2 == 1:
            print(i)


# 函数闭包:用于增强函数func:给函数func增加统计时间的功能:
def count_time_wrapper(func):
    def improved_func():
        start_time = time.time()
        func()
        end_time = time.time()
        print(f"It takes {end_time - start_time} S to find all the odds in range !!!")

    return improved_func


if __name__ == '__main__':
    # 调用count_time_wrapper增强函数
    print_odd = count_time_wrapper(print_odd)
    print_odd()

        闭包本质上是一个函数,闭包函数的传入参数和返回值都是函数,闭包函数得到返回值函数是对传入函数增强后的结果。

日志装饰器:

def log_wrapper(func):
    """
    闭包,用于增强函数func: 给func增加日志功能
    """

    def improved_func():
        start_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))  # 起始时间
        func()  # 执行函数
        end_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))  # 结束时间
        print("Logging: func:{} runs from {} to {}".format(func.__name__, start_time, end_time))

    return improved_func

二、装饰器使用方法:

        通过装饰器进行函数增强,只是一种语法糖,本质上跟上个程序(使用函数闭包)完全一致。

import time


# 函数闭包:用于增强函数func:给函数func增加统计时间的功能:
def count_time_wrapper(func):
    def improved_func():
        start_time = time.time()
        func()
        end_time = time.time()
        print(f"It takes {end_time - start_time} S to find all the odds in range !!!")

    return improved_func


# 控制台打印100以内的奇数:
@count_time_wrapper  # 添加装饰器
def print_odd():
    for i in range(100):
        if i % 2 == 1:
            print(i)


if __name__ == '__main__':
    # 使用  @装饰器(增强函数名) 给当前函数添加装饰器,等价于执行了下面这条语句:
    # print_odd = count_time_wrapper(print_odd)
    print_odd()

         装饰器在第一次调用被装饰函数时进行增强,只增强一次,下次调用仍然是调用增强后的函数,不会重复执行增强!

保留函数参数和返回值的函数闭包:

  • 之前所写的函数闭包,在增强主要功能函数时,没有保留原主要功能函数的参数列表和返回值。
  • 一个保留参数列表和返回值的函数闭包写法:
def general_wrapper(func):
    def improved_func(*args, **kwargs):
        # 增强函数功能:
        ret = func(*args, **kwargs)
        # 增强函数功能:
        return ret

    return improved_func

优化装饰器(参数传递、设置返回值): 

import time


# 函数闭包:用于增强函数func:给函数func增加统计时间的功能:
def count_time_wrapper(func):
    
    # 增强函数:
    def improved_func(*args, **kwargs):
        start_time = time.time()
        result = func(*args, **kwargs)
        end_time = time.time()
        print(f"It takes {end_time - start_time} S to find all the odds in range !!!")
        
        # 原函数返回值
        return result

    return improved_func


# 计算0-lim奇数之和:
@count_time_wrapper
def count_odds(lim):
    cnt = 0
    for i in range(lim):
        if i % 2 == 1:
            cnt = cnt + i

    return cnt


if __name__ == '__main__':
    result = count_odds(10000000)

    print(f"计算结果为{result}!")

三、多个装饰器的执行顺序:

# 装饰器1:
def wrapper1(func1):
    print("set func1")  # 在wrapper1装饰函数时输出

    def improved_func1(*args, **kwargs):
        print("call func1")  # 在wrapper1装饰过的函数被调用时输出
        func1(*args, **kwargs)
        return None

    return improved_func1


# 装饰器2:
def wrapper2(func2):
    print("set func2")  # 在wrapper2装饰函数时输出

    def improved_func2(*args, **kwargs):
        print("call func1")  # 在wrapper2装饰过的函数被调用时输出
        func2(*args, **kwargs)
        return None

    return improved_func2


@wrapper1
@wrapper2
def original_func():
    pass


if __name__ == '__main__':
    original_func()

    print("------------")

    original_func()

        这里得到的执行结果是,wrapper2装饰器先执行,原因是因为:程序从上往下执行,当运行到:

@wrapper1
@wrapper2
def original_func():
    pass

        这段代码时,使用函数闭包的方式解析为:

original_func = wrapper1(wrapper2(original_func))

        所以先进行wrapper2装饰,然后再对被wrapper2装饰完成的增强函数再由wrapper1进行装饰,返回最终的增强函数。


四、创建带参数的装饰器:

装饰器允许传入参数,一个携带了参数的装饰器将有三层函数,如下所示:

import functools


def log_with_param(text):
    def decorator(func):
        @functools.wraps(func)
        def wrapper(*args, **kwargs):
            print('call %s():' % func.__name__)
            print('args = {}'.format(*args))
            print('log_param = {}'.format(text))
            return func(*args, **kwargs)

        return wrapper

    return decorator


@log_with_param("param!!!")
def test_with_param(p):
    print(test_with_param.__name__)


if __name__ == '__main__':
    test_with_param("test")

将其 @语法 去除,恢复函数调用的形式:

# 传入装饰器的参数,并接收返回的decorator函数
decorator = log_with_param("param!!!")

# 传入test_with_param函数
wrapper = decorator(test_with_param)

# 调用装饰器函数
wrapper("I'm a param")

  • 9
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 11
    评论
评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爱吃糖的范同学

你的鼓励是我创作的最大动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值