functools.wraps
我们在使用 Decorator 的过程中,难免会损失一些原本的功能信息。直接拿 stackoverflow 里面的栗子
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
def logged(func):
def with_logging(*args, **kwargs):
print func.__name__ + " was called"
return func(*args, **kwargs)
return with_logging
@logged
def f(x):
"""does some math"""
return x + x * x
def f(x):
"""does some math"""
return x + x * x
f = logged(f)
In [24]: f.__name__
Out[24]: with_logging
|
而functools.wraps 则可以将原函数对象的指定属性复制给包装函数对象, 默认有 __module__、__name__、__doc__,或者通过参数选择。代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
from functools import wraps
def logged(func):
@wraps(func)
def with_logging(*args, **kwargs):
print func.__name__ + " was called"
return func(*args, **kwargs)
return with_logging
@logged
def f(x):
"""does some math"""
return x + x * x
print f.__name__ # prints 'f'
|
functools.wraps的更多相关文章
- python中functools.wraps装饰器的作用
functools.wraps装饰器用于显示被包裹的函数的名称 import functools def node(func): #@functools.wraps(func) def wrapped ...
- python functools.wraps装饰器模块
# -*-coding=utf-8 -*-#实现一个函数执行后计算执行时间的功能 __author__ = 'piay' import time, functools def foo(): ''' 定 ...
- 【Python】functools.wraps定义函数装饰器
第一次见到functools.wraps是在 Flask Web开发 中,一直不明白怎么回事. 装饰器(decorator)是干嘛的?对于受到封装的原函数来说,装饰器能够在那个函数执行前或者执行后分别 ...
- functools.wraps函数
原文地址:https://www.cnblogs.com/fcyworld/p/6239951.html 第一次见到functools.wraps是在 Flask Web开发 中,一直不明白怎么回事. ...
- python functools.wraps functools.partial实例解析
一:python functools.wraps 实例 1. 未使用wraps的实例 #!/usr/bin/env python # coding:utf-8 def logged(func): de ...
- 装饰器中的@functools.wraps的作用
def login_required(view_func): @functools.wraps(view_func) def wrapper(*args, **kwargs): ...... retu ...
- python functools.wraps
我们在使用装饰器的时候,有些函数的功能会丢失,比如func.__name__,func.__doc__,func.__module__ 比如下面这个例子: In [16]: def logged(fu ...
- functools.wraps 带参数的装饰器 多个装饰器装饰同一个函数
装饰器开发原则 : 开放封闭原则装饰器的作用 :在不改变原函数的调用方式的情况下,在函数的前后添加功能装饰器的本质 : 闭包函数 def wrapper(func): def inner(*args, ...
- python装饰器中functools.wraps的作用详解
直接上代码看效果: # 定义一个最简单的装饰器 def user_login_data(f): def wrapper(*args, **kwargs): return f(*args, **kwar ...
- Python 中实现装饰器时使用 @functools.wraps 的理由
Python 中使用装饰器对在运行期对函数进行一些外部功能的扩展.但是在使用过程中,由于装饰器的加入导致解释器认为函数本身发生了改变,在某些情况下——比如测试时——会导致一些问题.Python 通过 ...
随机推荐
- Restful 权限的思考
转自:https://cnodejs.org/topic/551802d3687c387d2f5b2906 基于RESTful API 怎么设计用户权限控制? 原文链接:简书 前言 有人说,每个人 ...
- SpringBoot程序启动时执行初始化代码
因项目集成了Redis缓存部分数据,需要在程序启动时将数据加载到Redis中,即初始化数据到Redis. 在SpringBoot项目下,即在容器初始化完毕后执行我们自己的初始化代码. 第一步:创建实现 ...
- android.useDeprecatedNdk=true
android.useDeprecatedNdk=true ndk{ moduleName "aa" abiFilter "armeabi-v7a" }
- 解决/usr/lib/libstdc++.so.6: version `GLIBCXX_3.4.17' not found问题
在项目中使用第三方动态库时,出现异常:/usr/lib64/libstdc++.so.6: version `GLIBCXX_3.4.17' not found 查看系统库时,发现确实没有对应的版本: ...
- AC日记——Little Elephant and Problem codeforces 221c
221C 思路: 水题: 代码: #include <cstdio> #include <cstring> #include <iostream> #include ...
- AC日记——Valued Keys codeforces 801B
801B - Valued Keys 思路: 水题... 来,上代码: #include <cstdio> #include <cstring> #include <io ...
- AC日记——[WC2013]糖果公园 cogs 1817
[WC2013]糖果公园 思路: 带修改树上莫队(模板): 来,上代码: #include <cmath> #include <cstdio> #include <cst ...
- luogu P1446 [HNOI2008]Cards
题目链接 luogu P1446 [HNOI2008]Cards 题解 题意就是求染色方案->等价类 洗牌方式构成成了一个置换群 然而,染色数限制不能用polay定理直接求解 考虑burnsid ...
- POJ 3041 Asteroids (二分图匹配)
[题目链接] http://poj.org/problem?id=3041 [题目大意] 一个棋盘上放着一些棋子 每次操作可以拿走一行上所有的棋子或者一列上所有的棋子 问几次操作可以拿完所有的棋子 [ ...
- 【bzoj1598】【 [Usaco2008 Mar]牛跑步】启发式搜索思路+spfa
(上不了p站我要死了,侵权度娘背锅) 最近复习搜索,先从启发式搜索来吧. 感觉启发式搜索这玩意挺玄学的,先从其思想入手,做一道经典的K短路. Description BESSIE准备用从牛棚跑到池塘的 ...