python代码一贯以优雅,简洁著称,而有时侯反而会让人难以理解,比如说wrapper(或者说decorator),这种方式提高了代码的可重用性,使用起来更简洁方便。

举个例子,比如WebApp常用的处理请求的Handler。

def show_page(request):
# do something
return response

有些时候,如果请求是POST方式发送过来的,可能你需要检查用户名和密码的合法性。这个时候,你不得不修改以上函数来做必要的认证处理:

def show_page(request):
authenticator.authenticate(request)
# do stuff
return response

但是这样做后,你发现你不想将代码植入到已经编好的功能中,因为那样做很stupid,如果每个需要认证的的地方都这样做,许多编号的handler都将改写。这似乎违背了代码的reusable。再者,从功能模块角度来看,认证似乎是一个辅助功能,不应该集成到Handler中。于是乎,使用decorators(或 wrappers)是一个不错的选择:

@authenticate
def show_page(request):
# do stuff
return response

一个简单的authentiate如下:

def authenticate(func):
def authenticate_and_call(*args, **kwargs):
if not Account.is_authentic(request):
raise Exception('Authentication Failed.')
return func(*args, **kwargs)
return authenticate_and_call

经过@authenticate处理后的show_page(request)函数等价于如下:

show_page = authenticate(show_page)

从上面的函数也可直观的看出为什么decorator又叫做wrapper了,很直观的类似包装了一层东西一样,升级为2.0版本。

python是函数式编程语言,函数可以作为参数传递或返回值返回。对于authenticate函数,在传入show_page后返回一个函数authenticate_and_call,也就是说show_page被重新指向了authenticate_and_call。

show_page = authenticate(*args, **kwargs)

这样一来,show_page加入了新的功能,完成了包装。他还是可以接收request参数,因为其参数是(*args, **kwargs)。

此处顺便提一下python的 *args参数,看下面例子:

def func1(x, y, z):
print x
print y
print z def func2(*args):
# Convert args tuple to a list so we can modify it
args = list(args)
args[0] = 'Hello'
args[1] = 'awesome'
func1(*args) func2('Goodbye', 'cruel', 'world!')
# Will print
# > Hello
# > awesome
# > world!

python中有很多库函数和自带函数参数中都包含*args, **kwargs,传递过来时就像一个"pack",而对于接收的函数来说可以"unpack"这些参数,做一些处理,例如:

  1. validate arguments before passing them on
  2. set defaults for positional arguments
  3. create adaptors for different pieces of code / libraries
  4. modify arguments depending on context
  5. log calls to methods
  6. write better and more resilient wrapper

将参数定义为“任意”的方式在decorator中很常用。

参考资料:

http://hangar.herokuapp.com/python/packing-unpacking-arguments

http://hangar.runway7.net/python/decorators-and-wrappers

Decorators and Wrappers in Python的更多相关文章

  1. 我实在不懂Python的Asyncio

    原语 事件循环(Event Loop) Awaitables和Coroutines Coroutine Wrappers Awaitables and Futures Tasks Handles Ex ...

  2. Python装饰器完全解读

    1 引言 装饰器(Decorators)可能是Python中最难掌握的概念之一了,也是最具Pythonic特色的技巧,深入理解并应用装饰器,你会更加感慨——人生苦短,我用Python. 2 初步理解装 ...

  3. python问答

    1)什么是Python?使用Python有什么好处? Python是一种编程语言,包含对象,模块,线程,异常和自动内存管理.Python的好处在于它简单易用,可移植,可扩展,内置数据结构,并且它是一个 ...

  4. TensorFlow源码框架 杂记

    一.为什么我们需要使用线程池技术(ThreadPool) 线程:采用“即时创建,即时销毁”策略,即接受请求后,创建一个新的线程,执行任务,完毕后,线程退出: 线程池:应用软件启动后,立即创建一定数量的 ...

  5. Django—— 缓存框架

    译者注:1.无用的,吹嘘的说辞不翻译:2.意译,很多地方不准确. 动态网站最为重要的一点就是好,网页是动态的.每一次用户请求页面,网站就要进行各种计算——从数据库查询,到render模板,到各种逻辑运 ...

  6. Easy Install详细参数

    Easy Install Easy Install is a python module (easy_install) bundled with setuptools that lets you au ...

  7. 使用Django.core.cache操作Memcached导致性能不稳定的分析过程

    使用Django.core.cache操作Memcached导致性能不稳定的分析过程 最近测试一项目,用到了Nginx缓存服务,那可真是快啊!2Gb带宽都轻易耗尽. 不过Api接口无法简单使用Ngin ...

  8. CentOS安全防护实例

    (1) 借助iptables的recent模块限制IP连接数 可以限制瞬间连接数过大的恶意IP(比如web应用防护,但不适用于LVS+Keepalived集群环境) 防护指令如下 # 允许一个客户端6 ...

  9. 【译】PEP 318--函数和方法的装饰器

    PEP原文 : https://www.python.org/dev/peps/pep-0318 PEP标题: Decorators for Functions and Methods PEP作者: ...

随机推荐

  1. SQL Cast()函数

    sql cast()函数 2010-09-17 13:30:26| 分类: Sql | 标签:sql case() 函数 |字号大中小 订阅 (1).CAST()函数的参数是一个表达式,它包括用AS关 ...

  2. How to Send an Email Using UTL_SMTP with Authenticated Mail Server. (文档 ID 885522.1)

    APPLIES TO: PL/SQL - Version 9.2.0.1 to 12.1.0.1 [Release 9.2 to 12.1]Information in this document a ...

  3. 获取iOS设备属性

    通过使用UIDevice: [[UIDevice currentDevice] systemName]; [[UIDevice currentDevice] systemVersion];//os v ...

  4. php7 install memcached extension

    #download source code package from git $ git clone https://github.com/php-memcached-dev/php-memcache ...

  5. leetcode Search in Rotated Sorted Array python

      #Suppose a sorted array is rotated at some pivot unknown to you beforehand. #(i.e., 0 1 2 4 5 6 7  ...

  6. leetcode Merge K sorted Lists python

    # Definition for singly-linked list. # class ListNode(object): # def __init__(self, x): # self.val = ...

  7. Struts2问题,已解决No result defined for action and result input

    struts2.1.8 必须在struts.xml中配置namespace属性 如果你在2.0中一切OK,但是在2.1中确出现了No result defined for action的异常,就是在因 ...

  8. Android Studio 添加Assets目录

    Android Studio 添加Assets目录: 法一: Since Android Studio uses the new Gradle-based build system, you shou ...

  9. js关闭 window.open 打开的页面

    1.关闭 当前页面 window.opener = null; window.open('', '_self', ''); window.close(); 但是在FF中就是不行: 2.项目中情况是通过 ...

  10. redis的内部实现机制

    一 理论基础 redis