详解Python函数式编程之装饰器
目录
- 一、装饰器的本质:
- 函数闭包(functionclosure):
- 二、装饰器使用方法:
- 保留函数参数和返回值的函数闭包:
- 三、多个装饰器的执行顺序:
- 四、创建带参数的装饰器:
- 总结
一、装饰器的本质: 装饰器(decorator)本质是函数闭包(function closure)的语法糖(Syntactic sugar)
函数闭包(function closure):
【详解Python函数式编程之装饰器】函数闭包是函数式语言(函数是一等公民,可作为变量使用)中的术语。函数闭包:一个函数,其参数和返回值都是函数,用于增强函数功能,面向切面编程(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_funcif __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 retreturn 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 resultreturn improved_func# 计算0-lim奇数之和:@count_time_wrapperdef count_odds(lim):cnt = 0for i in range(lim):if i % 2 == 1:cnt = cnt + ireturn cntif __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 Nonereturn improved_func1# 装饰器2:def wrapper2(func2):print("set func2")# 在wrapper2装饰函数时输出def improved_func2(*args, **kwargs):print("call func1")# 在wrapper2装饰过的函数被调用时输出func2(*args, **kwargs)return Nonereturn improved_func2@wrapper1@wrapper2def original_func():passif __name__ == '__main__':original_func()print("------------")original_func()

文章图片
这里得到的执行结果是,wrapper2装饰器先执行,原因是因为:程序从上往下执行,当运行到:
@wrapper1@wrapper2def original_func():pass
这段代码时,使用函数闭包的方式解析为:
original_func = wrapper1(wrapper2(original_func))
所以先进行wrapper2装饰,然后再对被wrapper2装饰完成的增强函数再由wrapper1进行装饰,返回最终的增强函数。

文章图片
四、创建带参数的装饰器: 装饰器允许传入参数,一个携带了参数的装饰器将有三层函数,如下所示:
import functoolsdef 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 wrapperreturn 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")
总结 本篇文章就到这里了,希望能够给你带来帮助,也希望您能够多多关注脚本之家的更多内容!
推荐阅读
- 用Python实现屏幕截图详解
- python|卧槽,做Python兼职又接了一个大单,真香
- python|部署证明书提出了挑战和架构正统观念
- 学习|大学入坑Python,靠接单3天赚了1000块,方法让人钦佩
- 3个案例,详解如何选择合适的研发模式|3个案例,详解如何选择合适的研发模式 | 研发效能提升36计
- JavaScript中常见的高阶函数总结
- C/C++的文件IO函数你知道吗
- 浅谈Python数据处理csv的应用小结
- python|3-张量API-下
- 爬虫|Python爬虫(批量爬取变形金刚图片,下载保存到本地。)