前言

对python的修饰器的理解一直停留在"使用修饰器把函数注册为事件的处理程序"的层次,也是一知半解;这样拖着不是办法,索性今天好好整理一下关于python修饰器的概念及用法。

介绍

装饰器是一个很著名的设计模式,经常被用于有切面需求的场景,较为经典的有插入日志、性能测试、事务处理等。装饰器是解决这类问题的绝佳设计,有了装饰器,我们就可以抽离出大量函数中与函数功能本身无关的雷同代码并继续重用。概括的讲,装饰器的作用就是为已经存在的对象添加额外的功能。

功能

我们首先从一个简单的例子说起,这个例子是stackflow上的一个问题,如何通过使用如下的代码实现输出<b><i>Hello</i></b>:

@makebold
@makeitalic
def say():
return "Hello"

先看一下答案:

def makebold(fn):
def wrapped():
return "<b>" + fn() + "</b>"
return wrapped def makeitalic(fn):
def wrapped():
return "<i>" + fn() + "</i>"
return wrapped @makebold
@makeitalic
def hello():
return "hello world" print hello() ## 返回 <b><i>hello world</i></b>

这里的@makebold@makeitalic似乎给Hello加上了一层包装(or修饰),这就是修饰器最明显的体现。

从需求谈起

初期,我写了一个函数

def foo():
print 'in foo()'
foo()

为了检查这个函数的复杂度(在网络编程中程序的延时还是很重要的),需要测算运算时间,增加了计算时间的功能有了下面的代码:

import time
def foo():
start = time.clock()
print 'in foo()'
end = time.clock()
print 'Time Elapsed:', end - start foo()

这里只是写了一个函数,如果我想测量多个函数的延时,由于必须知道start与end,所以必须写在程序的开头与结尾,难道每一个程序都这样复制粘贴么?固然可行,但是,我们可以通过设计模式中将功能与数据部分分离一样,将这个测量时间的函数分离出去,就像C++中我们可以将这个测量时间的函数变为一个类,通过调用这个类,赋予不同的函数来测量不同的函数的运行时长。在python中,由于函数实际上就是对象,所以可以利用类似的方法实现:

import time  

def foo():
print 'in foo()' def timeit(func):
start = time.clock()
func()
end =time.clock()
print 'Time Elapsed:', end - start timeit(foo)

这里func()就可以指定函数了,但是如果我不想填这个函数或者这个功能函数并不能修改成类似的形式怎么办?我们需要的是最大限度的少改动:

import time  

def foo():
print 'in foo()' # 定义一个计时器,传入一个,并返回另一个附加了计时功能的方法
def timeit(func): # 定义一个内嵌的包装函数,给传入的函数加上计时功能的包装
def wrapper():
start = time.clock()
func()
end =time.clock()
print 'Time Elapsed:', end - start # 将包装后的函数返回
return wrapper foo = timeit(foo) #可以直接写成@timeit + foo定义,python的"语法糖"
foo()

在这个代码中,timeit(foo)不是直接产生调用效果,而是返回一个与foo参数列表一致的函数,此时此foo非彼foo!因为此时的foo具有了timeit的功效,简单来说就是能够让你在装饰前后执行代码而无须改变函数本身内容,装饰器是一个函数,而其参数为另外一个函数。

一个有趣的"汉堡"让你了解顺序

顺序在修饰器还是非常重要的,利用一个代码展示一下:

def bread(func) :
def wrapper() :
print "</''' '''\>"
func()
print "<\______/>"
return wrapper def ingredients(func) :
def wrapper() :
print "#tomatoes#"
func()
print "~salad~"
return wrapper def sandwich(food="--ham--") :
print food sandwich()
#输出 : --ham--
sandwich = bread(ingredients(sandwich))
sandwich() #输出: #</''' '''\>
# #tomatoes#
# --ham--
# ~salad~
#<\______/>

加上语法糖,代码可以更简洁:

def bread(func) :
def wrapper() :
print "</''' '''\>"
func()
print "<\______/>"
return wrapper def ingredients(func) :
def wrapper() :
print "#tomatoes#"
func()
print "~salad~"
return wrapper @bread
@ingredients
def sandwich(food="--ham--") :
print food sandwich()

拓展

内置修饰器

内置的装饰器有三个,分别是staticmethod、classmethod和property,作用分别是把类中定义的实例方法变成静态方法、类方法和类属性。

对有参函数进行修饰

一个参数

如果原函数有参数,那闭包函数必须保持参数个数一致,并且将参数传递给原方法

def w1(fun):
def wrapper(name):
print("this is the wrapper head")
fun(name)
print("this is the wrapper end")
return wrapper @w1
def hello(name):
print("hello"+name) hello("world") # 输出:
# this is the wrapper head
# helloworld
# this is the wrapper end

多个参数测试:

def w2(fun):
def wrapper(*args,**kwargs):
print("this is the wrapper head")
fun(*args,**kwargs)
print("this is the wrapper end")
return wrapper @w2
def hello(name,name2):
print("hello"+name+name2) hello("world","!!!") #输出:
# this is the wrapper head
# helloworld!!!
# this is the wrapper end

有返回值的函数


def w3(fun):
def wrapper():
print("this is the wrapper head")
temp=fun()
print("this is the wrapper end")
return temp #要把值传回去呀!!
return wrapper @w3
def hello():
print("hello")
return "test" result=hello()
print("After the wrapper,I accept %s" %result) #输出:
#this is the wrapper head
#hello
#this is the wrapper end
#After the wrapper,I accept test

有参数的修饰器

直接上代码:

def func_args(pre='xiaoqiang'):
def w_test_log(func):
def inner():
print('...记录日志...visitor is %s' % pre)
func() return inner return w_test_log # 带有参数的修饰器能够起到在运行时,有不同的功能 # 先执行func_args('wangcai'),返回w_test_log函数的引用
# @w_test_log
# 使用@w_test_log对test_log进行修饰
@func_args('wangcai')
def test_log():
print('this is test log') test_log() #输出:
#...记录日志...visitor is wangcai
# this is test log

通用修饰器

对每个类型都有一个修饰器形式,怎么记得下来?所以就有了这个"万能修饰器":

def w_test(func):
def inner(*args, **kwargs):
ret = func(*args, **kwargs)
return ret return inner @w_test
def test():
print('test called') @w_test
def test1():
print('test1 called')
return 'python' @w_test
def test2(a):
print('test2 called and value is %d ' % a) test()
test1()
test2(9) # 输出:
#test called
#test1 called
#test2 called and value is 9

类修饰器

当创建一个对象后,直接去执行这个对象,那么是会抛出异常的,因为他不是callable,无法直接执行,但进行修改后,就可以直接执行调用:

class Test(object):
def __call__(self, *args, **kwargs):
print('call called') t = Test()
print(t())
# 就可以直接执行

直接对类进行修饰:

class Test(object):
def __init__(self, func):
print('test init')
print('func name is %s ' % func.__name__)
self.__func = func def __call__(self, *args, **kwargs):
print('this is wrapper')
self.__func() @Test
def test():
print('this is test func') test() #输出:
# test init
# func name is test
# this is wrapper
# this is test func

后记

先介绍到这里,大致也对修饰器有了一定的理解。后面自己会结合自己的项目继续深入学习。

谈谈python修饰器的更多相关文章

  1. Python修饰器的函数式编程

    Python的修饰器的英文名叫Decorator,当你看到这个英文名的时候,你可能会把其跟Design Pattern里的Decorator搞混了,其实这是完全不同的两个东西.虽然好像,他们要干的事都 ...

  2. Python修饰器

    Python的修饰器的英文名叫Decorator,当你看到这个英文名的时候,你可能会把其跟Design Pattern里的Decorator搞混了,其实这是完全不同的两个东西.虽然好像,他们要干的事都 ...

  3. Python修饰器的函数式编程(转)

    From:http://coolshell.cn/articles/11265.html 作者:陈皓 Python的修饰器的英文名叫Decorator,当你看到这个英文名的时候,你可能会把其跟Desi ...

  4. python 修饰器 最好的讲解

    Python的修饰器的英文名叫Decorator,修饰器就是对一个已有的模块做一些“修饰工作”,比如在现有的模块加上一些小装饰(一些小功能,这些小功能可能好多模块都会用到),但又不让这个小装饰(小功能 ...

  5. python修饰器(装饰器)以及wraps

    Python装饰器(decorator)是在程序开发中经常使用到的功能,合理使用装饰器,能让我们的程序如虎添翼. 装饰器的引入 初期及问题的诞生 假如现在在一个公司,有A B C三个业务部门,还有S一 ...

  6. python 修饰器(decorator)

    转载:Python之修饰器 - 知乎 (zhihu.com) 什么是修饰器,为什么叫修饰器 修饰器英文是Decorator, 我们假设这样一种场景:古老的代码中有几个很是复杂的函数F1.F2.F3.. ...

  7. Python 修饰器

    描述:对于函数foo,使用修饰器修饰,在执行foo函数的同时统计执行时间.这样其他函数都可以使用此修饰器得到运行时间. (有返回值和没有返回值的函数要用不同的修饰器似乎) (对于有返回值的函数,不确定 ...

  8. Python修饰器讲解

    转自:http://www.cnblogs.com/rollenholt/archive/2012/05/02/2479833.html 文章先由stackoverflow上面的一个问题引起吧,如果使 ...

  9. python修饰器各种实用方法

    This page is meant to be a central repository of decorator code pieces, whether useful or not <wi ...

随机推荐

  1. [转]_int64、long long 的区别

    大学生程序代写 C++的64位整数[原]by 赤兔 http://baike.baidu.com/view/1235293.htm 在做ACM题时,经常都会遇到一些比较大的整数.而常用的内置整数类型常 ...

  2. SHOI2016 随机序列

    给你一个数列,在相邻两个数之间插入加号,减号或乘号 每次支持单点修改,求所有这样可以得到的表达式之和,膜1e9 + 7 sol: 我是个 sb ... 可以发现,如果某位置出现了加号,后面一定有一个减 ...

  3. 图的Tarjan算法

    “Tarjan有三种算法 你们知道吗”——Tar乙己 void tarjan(int x) { low[x]=dfn[x]=++ind; q[++top]=x;mark[x]=; for(int i= ...

  4. SPOJ705 Distinct Substrings (后缀自动机&后缀数组)

    Given a string, we need to find the total number of its distinct substrings. Input T- number of test ...

  5. LeetCode:Add Digits - 非负整数各位相加

    1.题目名称 Add Digits (非负整数各位相加) 2.题目地址 https://leetcode.com/problems/add-digits/ 3.题目内容 英文:Given a non- ...

  6. redisCheckMem脚本

    最近维护的redis cluster需要扫描每个实例的内存使用率,首先我们需要获取实例已经使用的内存,获取实例的最大内存配额,两个值相比就能获取到内存使用比例. 实例的最大内存获取方法: $REDIS ...

  7. ACM学习历程—POJ3090 Visible Lattice Points(容斥原理 || 莫比乌斯)

    Description A lattice point (x, y) in the first quadrant (x and y are integers greater than or equal ...

  8. Linux IO实时监控iostat命令详解(转)

    简介 iostat主要用于监控系统设备的IO负载情况,iostat首次运行时显示自系统启动开始的各项统计信息,之后运行iostat将显示自上次运行该命令以后的统计信息.用户可以通过指定统计的次数和时间 ...

  9. JAVAmap容器基本使用

    import java.io.PrintWriter; import java.util.HashMap; import java.util.Map; import java.util.Scanner ...

  10. Project Online JS 添加Ribbon按钮

    var Projects = Projects || {}; (function () { Projects.ribbonButtonClick = function (name) { var pro ...