Python 装饰器之 functools.wraps
在看 Bottle 代码中看见 functools.wraps 这种用法。
def make_default_app_wrapper(name):
""" Return a callable that relays calls to the current default app. """
a = getattr(Bottle, name)
@functools.wraps(getattr(Bottle, name))
def wrapper(*a, **ka):
return getattr(app(), name)(*a, **ka)
return wrapper
之前没有看过,于是查文档了解了一下他的用处 先下定义: functools.wraps 是 ``装饰器``的``装饰器``
要明白 functiools.wraps 首先要明白 Python 的 Decorator
Decorator
在以前的 Blog 中曾经简单写过 Decorator。这次需要讲的更细一些。
Decorator 通过返回包装对象实现间接调用,以此插入额外逻辑。是从老大那边偷来的哪里摘抄来的,应该算是言简意赅了。
@dec2
@dec1
def func(arg1, arg2, ...):
pass
可以还原成
def func(arg1, arg2, ...):
pass
func = dec2(dec1(func))
@decomaker(argA, argB, ...)
def func(arg1, arg2, ...):
pass
可以还原成
func = decomaker(argA, argB, ...)(func)
In [1]: def outer(func):
...: def inner():
...: print "before func"
...: ret = func()
...: return ret + 1
...: return inner #返回 inner 函数对象
...: In [2]: @outer # 解释器执⾏行 foo = outer(foo)
...: def foo():
...: return 1
...: In [3]: foo
Out[3]: <function __main__.inner> In [4]: foo()
before func
Out[4]: 2
这个过程中执行了下面几步
- 函数 foo 作为 装饰器 outer 的参数被传入
- 函数 inner 对 func 进行调用,然后装饰器 outer 返回 inner
- 原来的函数名 foo 关联到 inner,如上面的foo <function __main__.inner> 所示,调用 foo 时间上是在调用 inner
装饰器不仅可以用函数返回包装对象,也可以是个类,不过这种方法太尼玛啰嗦,这里就不介绍了,想了解的自己去翻吧。下面我们写一个有点用处的 Decorator。 假想我们有个coordinate类,而且这个类提供了 x, y坐标,而我们要对两个coordinate 对象进行计算。代码如下:
class Coordinate(object):
def __init__(self, x, y):
self.x = x
self.y = y
def __repr__(self):
return "Coord: " + str(self.__dict__) def add(a, b):
return Coordinate(a.x + b.x, a.y + b.y) def sub(a, b):
return Coordinate(a.x - b.x, a.y - b.y) In [8]: one = Coordinate(100, 200) In [9]: two = Coordinate(300, 200) In [10]: three = Coordinate(-100, -100) In [11]: sub(one, three)
Out[11]: Coord: {'y': 300, 'x': 200} In [12]: add(one, three)
Out[12]: Coord: {'y': 100, 'x': 0} In [13]: sub(one, two)
Out[13]: Coord: {'y': 0, 'x': -200}
上面例子中的sub(one, two)与three都有负数,当我们把坐标限制在第一象限时,这两个就不符合我们的要求,用 Decorator 来做一个检测再好不过了
In [14]: def wrapper(func):
....: def checker(a, b):
....: if a.x < 0 or a.y < 0:
....: a = Coordinate(a.x if a.x > 0 else 0, a.y if a.y > 0 else 0)
....: if b.x < 0 or b.y < 0:
....: b = Coordinate(b.x if b.x > 0 else 0, b.y if b.y > 0 else 0)
....: ret = func(a, b)
....: if ret.x < 0 or ret.y <0:
....: ret = Coordinate(ret.x if ret.x > 0 else 0, ret.y if ret.y > 0 else 0)
....: return ret
....: return checker
....:
In [16]: @wrapper
....: def add(a, b):
....: return Coordinate(a.x + b.x, a.y + b.y)
....: In [17]: @wrapper
....: def sub(a, b):
....: return Coordinate(a.x - b.x, a.y + b.y)
....: In [18]: add(one, three)
Out[18]: Coord: {'y': 200, 'x': 100} In [19]: one
Out[19]: Coord: {'y': 200, 'x': 100} In [20]: sub(one, two)
Out[20]: Coord: {'y': 400, 'x': 0}
这样,只计算的函数add与sub前面加一个 Decorator 就可以完成坐标的校验。比在函数内实现要优雅一些。
Decorator 还可以为类增加额外的成员,
In [21]: def hello(cls):
....: cls.hello = staticmethod(lambda: "HELLO")
....: return cls
....: In [22]: @hello
....: class World(object):pass
....: In [23]: World.hello
Out[23]: <function __main__.<lambda>> In [24]: World.hello()
Out[24]: 'HELLO'
functools.wraps
我们在使用 Decorator 的过程中,难免会损失一些原本的功能信息。直接拿 stackoverflow 里面的栗子
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__,或者通过参数选择。代码如下:
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'
print f.__doc__ # prints 'does some math'
from:http://sunisdown.me/python-zhuang-shi-qi-zhi-functoolswraps.html
Python 装饰器之 functools.wraps的更多相关文章
- Python装饰器之functools.wraps的作用
# -*- coding: utf-8 -*- # author:baoshan def wrapper(func): def inner_function(): pass return inner_ ...
- python装饰器中@wraps作用--修复被装饰后的函数名等属性的改变
Python装饰器(decorator)在实现的时候,被装饰后的函数其实已经是另外一个函数了(函数名等函数属性会发生改变),为了不影响,Python的functools包中提供了一个叫wraps的de ...
- python装饰器的wraps作用
不加: from functools import wraps def my_decorator(func): def wper(*args, **kwargs): '''decorator''' p ...
- Python装饰器之 property()
1. 何为装饰器? 官方定义:装饰器是一个很著名的设计模式,经常被用于有切面需求的场景,较为经典的有插入日志.性能测试.事务处理等.装饰器是解决这类问题的绝佳设计,有了装饰器,我们就可以抽离出大量函数 ...
- python 装饰器之应用示例
import time import hashlib import pickle import threading #装饰函数缓存应用 cache ={} def is_obsolete(entry, ...
- python装饰器之函数作用域
1.函数作用域LEGB L:local函数内部作用域 E:enclosing函数内部与内嵌函数之间 G:global全局作用域 B:build-in内置作用域 passline = 60 def fu ...
- python装饰器之使用情景分析
http://blog.csdn.net/yueguanghaidao/article/details/10089181
- Python装饰器详解
python中的装饰器是一个用得非常多的东西,我们可以把一些特定的方法.通用的方法写成一个个装饰器,这就为调用这些方法提供一个非常大的便利,如此提高我们代码的可读性以及简洁性,以及可扩展性. 在学习p ...
- Python 装饰器学习心得
最近打算重新开始记录自己的学习过程,于是就捡起被自己废弃了一年多的博客.这篇学习笔记主要是记录近来看的有关Python装饰器的东西. 0. 什么是装饰器? 本质上来说,装饰器其实就是一个特殊功能的函数 ...
随机推荐
- 05.vue-resource的基本使用
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- [Git] 009 逆转未来
1. 想逆转未来,得先知道时间线 1.1 git log 1.2 git log --oneline 此命令的显示结果比 git log 简洁 1.3 git reflog 此命令显示的记录比前两者完 ...
- Samba服务问答
1. samba服务用在什么地方?samba服务用于把Linux服务器上的文件或者打印接共享给windows或者Linux. 2. 在samba服务的配置文件中,[global]配置部分的securi ...
- 从建立yum仓库到搭建ftp以及http服务
1 什么是yum仓库 yum工作需要依赖C/S架构工作模式的文件服务器,服务器中存放了yum工作时所需的程序包.yum接收到需要安装的程序包的名称之后,通过文件共享协议(或者文件传输协议),在配置文件 ...
- 在eclipse上配置多个jdk
在实际生产中,可能会遇到这样的问题:在eclipse中存在多个项目时,可能不同的项目需要不同的jdk版本.这时,我们就可以给eclipse配置多个jdk进行切换. 注:貌似只有较新版eclipse才能 ...
- ES6判断当前页面是否微信浏览器中打开
1.使用jq判断是否用微信浏览器打开页面 var is_weixin = (function(){return navigator.userAgent.toLowerCase().indexOf('m ...
- jquery获取年月日时分秒当前时间
获取年月日时分秒的当前时间,可按照某种格式显示出来,下面是一种得到如2017年02月02日 00:00:00格式的当前时间 function getCurrentDate(date){ var y ...
- 加秘钥的SSH
import paramiko private_key = paramiko.RSAKey.from_private_key_file('id_rsa31.txt') # 创建SSH对象 ssh = ...
- win32 socket编程(四)——服务器端实例(TCP)
//Server服务器端Server.cpp 1 // 定义控制台应用程序的入口点. // #include "stdafx.h" #include <winsock2.h& ...
- 提升JAVA代码的好“味道”
让代码性能更高 需要 Map 的主键和取值时,应该迭代 entrySet() 当循环中只需要 Map 的主键时,迭代 keySet() 是正确的.但是,当需要主键和取值时,迭代 entrySet() ...