装饰器中functools的用处
定义一个最简单的装饰器
def user_login_data(f):
def wrapper(*args, **kwargs):
return f(*args, **kwargs) return wrapper
# 用装饰器装饰以下两个函数
@user_login_data
def num1():
print("aaa") @user_login_data
def num2():
print("bbbb") if __name__ == '__main__':
print(num1.__name__)
print(num2.__name__)
以上代码的输出结果为:
wrapper wrapper
由此函数使用装饰器时,函数的函数名即 __name__已经被装饰器改变.
一般定义装饰器的话可以不用考虑这点,但是如果多个函数被两个装饰器装饰时就报错,因为两个函数名一样,第二个函数再去装饰的话就报错.
解决方案就是引入 functools.wraps ,以上代码的解决如下:
def user_login_data(f):
@functools.wraps(f)
def wrapper(*args, **kwargs):
return f(*args, **kwargs) return wrapper
增加@functools.wraps(f), 可以保持当前装饰器去装饰的函数的 name 的值不变
以上输出结果就是:
num1 num2
Python装饰器(decorator)在实现的时候,有一些细节需要被注意。例如,被装饰后的函数其实已经是另外一个函数了(函数名等函数属性会发生改变)。这样有时候会对程序造成一些不便,例如笔者想对flask框架中的一些函数添加自定义的decorator,添加后由于函数名和函数的doc发生了改变,对测试结果有一些影响。
所以,Python的functools包中提供了一个叫wraps的decorator来消除这样的副作用。写一个decorator的时候,最好在实现之前加上functools的wrap,它能保留原有函数的名称和docstring。
装饰器中functools的用处的更多相关文章
- python装饰器中functools.wraps的作用详解
直接上代码看效果: # 定义一个最简单的装饰器 def user_login_data(f): def wrapper(*args, **kwargs): return f(*args, **kwar ...
- python_如何修改装饰器中参数?
案例: 为分析程序内哪些函数执行时间开销较大,我们需定义一个带timeout参数的装饰器 需求: 统计被装饰函数的运行时间 时间大于timeout时,将此次函数调用记录到log日志中 运行时可以修改t ...
- JS中bind、call和apply的作用以及在TS装饰器中的用法
目录 1,前言 1,call 1.1,例子 1.2,直接调用 1.3,将this指向另一个对象 1.4,传递参数 2,apply 2.1,例子 2.2,直接调用 2.3,将this指向另一个对象 2. ...
- 装饰器中的@functools.wraps的作用
def login_required(view_func): @functools.wraps(view_func) def wrapper(*args, **kwargs): ...... retu ...
- python 装饰器和 functools 模块
转自:http://blog.jkey.lu/2013/03/15/python-decorator-and-functools-module/ 什么是装饰器? 在 python 语言里第一次看到装饰 ...
- python装饰器中@wraps作用--修复被装饰后的函数名等属性的改变
Python装饰器(decorator)在实现的时候,被装饰后的函数其实已经是另外一个函数了(函数名等函数属性会发生改变),为了不影响,Python的functools包中提供了一个叫wraps的de ...
- 【转】odoo 新API装饰器中one、model、multi的区别
http://blog.csdn.net/qq_18863573/article/details/51114893 1.one装饰器详解 odoo新API中定义方式: date=fields.Date ...
- python装饰器中的计时器thd.strat用法
thd = KThread(target=_new_func, args=(), kwargs=new_kwargs) thd.start() thd.join(seconds) alive = th ...
- 【Python】functools.wraps定义函数装饰器
第一次见到functools.wraps是在 Flask Web开发 中,一直不明白怎么回事. 装饰器(decorator)是干嘛的?对于受到封装的原函数来说,装饰器能够在那个函数执行前或者执行后分别 ...
随机推荐
- libpng warning:iCCP:known incorrect sRGB profile
原因是新版的libpng增强了检查,发出警告.此警告可以忽略.若要消除此警告则要使用v4的色彩配置.GIMP sRGB v4 色彩配置,修改当前图片的色彩配置,设为默认. sRGB profilesO ...
- vue 使用 echart ,自定义样式案例
1.vue 安装 echart 库 npm install echarts --save 2.vue代码 引入 let echarts = require("echarts/lib/echa ...
- 解析NaN
此文为自译文,且第一次翻译,有不足之处. 原英文地址:https://en.wikipedia.org/wiki/NaN 我的理解 32位下二进制的 NaN 存储格式为s111 1111 1111 1 ...
- nginx安装第三方模块echo-nginx-module
cd ~ wget -S https://github.com/agentzh/echo-nginx-module/archive/master.zip mv master echo-nginx-mo ...
- django中安装pillow ValueError: zlib is required unless explicitly disabled using --disable-zlib, aborting
在windows系统上,使用 pip install pillow安装pillow时 报错 在使用 easy_install Pillow 方式安装成功,默认是最高版本 如果需要在安装时,指定安装版 ...
- netframework webapi IogAttribute记录request参数和错误信息
参考博客 https://www.cnblogs.com/hnsongbiao/p/7039666.html 书写LogFilterAttribute public class LogFilterAt ...
- legend3---13、vue是真的好用
legend3---13.vue是真的好用 一.总结 一句话总结: 下次前端所有的交互页面都可以用vue 1.chrome查看post请求携带的参数? 请求的Headers里面的Form Data里面 ...
- C# Under the Hood: async/await (Marko Papic)
https://www.markopapic.com/csharp-under-the-hood-async-await/ Async and await keywords came with C# ...
- spark-submit 参数总结
spark-submit 可以提交任务到 spark 集群执行,也可以提交到 hadoop 的 yarn 集群执行. 1)./spark-shell --help :不知道如何使用,可通过它查看命 ...
- c++ STL 最大值最小值
#include <iostream>#include <algorithm>#include <deque> using namespace std; //二元谓 ...