作者:xlzd
链接:http://www.zhihu.com/question/26930016/answer/81263287
来源:知乎
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。简单来讲,可以不严谨地把Python的装饰器看做一个包装函数的函数。

比如,有一个函数:

<span class="k">def</span> <span class="nf">func</span><span class="p">():</span>
    <span class="k">print</span> <span class="s">'func() run.'</span>

<span class="k">if</span> <span class="s">'__main__'</span> <span class="o">==</span> <span class="n">__name__</span><span class="p">:</span>
    <span class="n">func</span><span class="p">()</span>

运行后将输出:

<span class=“go”>func() run.</span>

现在需要在函数运行前后打印一条日志, 但是又不希望或者没有权限修改函数内部的结构, 就可以用到装饰器(decorator):

<span class=“k”>def</span> <span class=“nf”>log</span><span class=“p”>(</span><span class=“n”>function</span><span class=“p”>):</span>
    <span class=“k”>def</span> <span class=“nf”>wrapper</span><span class=“p”>(</span><span class=“o”>*</span><span class=“n”>args</span><span class=“p”>,</span> <span class=“o”></span><span class=“n”>kwargs</span><span class=“p”>):</span>
        <span class=“k”>print</span> <span class=“s”>‘before function [</span><span class=“si”>%s</span><span class=“s”>()] run.’</span> <span class=“o”>%</span> <span class=“n”>function</span><span class=“o”>.</span><span class=“n”>name</span>
        <span class=“n”>rst</span> <span class=“o”>=</span> <span class=“n”>function</span><span class=“p”>(</span><span class=“o”>*</span><span class=“n”>args</span><span class=“p”>,</span> <span class=“o”></span><span class=“n”>kwargs</span><span class=“p”>)</span>
        <span class=“k”>print</span> <span class=“s”>‘after function [</span><span class=“si”>%s</span><span class=“s”>()] run.’</span> <span class=“o”>%</span> <span class=“n”>function</span><span class=“o”>.</span><span class=“n”>name</span>
        <span class=“k”>return</span> <span class=“n”>rst</span>
    <span class=“k”>return</span> <span class=“n”>wrapper</span>

<span class=“nd”>@log</span> <span class=“k”>def</span> <span class=“nf”>func</span><span class=“p”>():</span> <span class=“k”>print</span> <span class=“s”>‘func() run.’</span>

<span class=“k”>if</span> <span class=“s”>’main‘</span> <span class=“o”>==</span> <span class=“n”>name</span><span class=“p”>:</span> <span class=“n”>func</span><span class=“p”>()</span>

对于原来的函数”func()“并没有做修改,而是给其使用了装饰器log,运行后的输出为:

<span class=“n”>before</span> <span class=“n”>function</span> <span class=“p”>[</span><span class=“n”>func</span><span class=“p”>()]</span> <span class=“n”>run</span><span class=“o”>.</span>
<span class=“n”>func</span><span class=“p”>()</span> <span class=“n”>run</span><span class=“o”>.</span>
<span class=“n”>after</span> <span class=“n”>function</span> <span class=“p”>[</span><span class=“n”>func</span><span class=“p”>()]</span> <span class=“n”>run</span><span class=“o”>.</span>

把”@log”放到func()函数定义的地方,相当于执行了如下语句:

<span class=“n”>func</span> <span class=“o”>=</span> <span class=“n”>log</span><span class=“p”>(</span><span class=“n”>func</span><span class=“p”>)</span>

因为log()返回了一个函数, 所以原本的func指向了log()返回的函数wrapper。wrapper的参数列表为(*args, **kwargs), 所以其可以接受所有的参数调用, 在wrapper中,先打印了一行

‘before function [%s()] run.’ % function.name

(在Python中函数也是对象,函数的name是它的名字),然后执行了原来的函数并记录了返回值,在输出

‘after function [%s()] run.’ % function.name

后返回了函数的执行结果。

如果decorator本身需要传入参数,那就需要编写一个返回decorator的decorator。比如在Flask中:

@app.route(‘/’)
def index():
    return ‘hello, world!’

实现如下:

<span class=“kn”>import</span> <span class=“nn”>functools</span>

<span class=“k”>def</span> <span class=“nf”>log</span><span class=“p”>(</span><span class=“n”>text</span><span class=“o”>=</span><span class=“s”>”</span><span class=“p”>):</span> <span class=“k”>def</span> <span class=“nf”>decorator</span><span class=“p”>(</span><span class=“n”>function</span><span class=“p”>):</span> <span class=“nd”>@functools.wraps</span><span class=“p”>(</span><span class=“n”>function</span><span class=“p”>)</span> <span class=“k”>def</span> <span class=“nf”>wrapper</span><span class=“p”>(</span><span class=“o”>*</span><span class=“n”>args</span><span class=“p”>,</span> <span class=“o”></span><span class=“n”>kwargs</span><span class=“p”>):</span> <span class=“k”>print</span> <span class=“s”>‘before function [</span><span class=“si”>%s</span><span class=“s”>()] run, text: [</span><span class=“si”>%s</span><span class=“s”>].’</span> <span class=“o”>%</span> <span class=“p”>(</span><span class=“n”>function</span><span class=“o”>.</span><span class=“n”>name</span><span class=“p”>,</span> <span class=“n”>text</span><span class=“p”>)</span> <span class=“n”>rst</span> <span class=“o”>=</span> <span class=“n”>function</span><span class=“p”>(</span><span class=“o”>*</span><span class=“n”>args</span><span class=“p”>,</span> <span class=“o”></span><span class=“n”>kwargs</span><span class=“p”>)</span> <span class=“k”>print</span> <span class=“s”>‘after function [</span><span class=“si”>%s</span><span class=“s”>()] run, text: [</span><span class=“si”>%s</span><span class=“s”>].’</span> <span class=“o”>%</span> <span class=“p”>(</span><span class=“n”>function</span><span class=“o”>.</span><span class=“n”>name</span><span class=“p”>,</span> <span class=“n”>text</span><span class=“p”>)</span> <span class=“k”>return</span> <span class=“n”>rst</span> <span class=“k”>return</span> <span class=“n”>wrapper</span> <span class=“k”>return</span> <span class=“n”>decorator</span>

<span class=“nd”>@log</span><span class=“p”>(</span><span class=“s”>‘log text’</span><span class=“p”>)</span> <span class=“k”>def</span> <span class=“nf”>func</span><span class=“p”>():</span> <span class=“k”>print</span> <span class=“s”>‘func() run.’</span>

<span class=“k”>if</span> <span class=“s”>’main‘</span> <span class=“o”>==</span> <span class=“n”>name</span><span class=“p”>:</span> <span class=“n”>func</span><span class=“p”>()</span>

输出如下:

<span class=“go”>before function [func()] run, text: [log text].</span>
<span class=“go”>func() run.</span>
<span class=“go”>after function [func()] run, text: [log text].</span>

最后脑洞小开一下, 有没有办法实现既支持不带参数(如log), 又支持带参数(如log(‘text’))的decorator吗?

<span class=“kn”>import</span> <span class=“nn”>functools</span>

<span class=“k”>def</span> <span class=“nf”>log</span><span class=“p”>(</span><span class=“n”>argument</span><span class=“p”>):</span> <span class=“k”>if</span> <span class=“ow”>not</span> <span class=“nb”>callable</span><span class=“p”>(</span><span class=“n”>argument</span><span class=“p”>):</span> <span class=“k”>def</span> <span class=“nf”>decorator</span><span class=“p”>(</span><span class=“n”>function</span><span class=“p”>):</span> <span class=“nd”>@functools.wraps</span><span class=“p”>(</span><span class=“n”>function</span><span class=“p”>)</span> <span class=“k”>def</span> <span class=“nf”>wrapper</span><span class=“p”>(</span><span class=“o”>*</span><span class=“n”>args</span><span class=“p”>,</span> <span class=“o”></span><span class=“n”>kwargs</span><span class=“p”>):</span> <span class=“k”>print</span> <span class=“s”>‘before function [</span><span class=“si”>%s</span><span class=“s”>()] run, text: [</span><span class=“si”>%s</span><span class=“s”>].’</span> <span class=“o”>%</span> <span class=“p”>(</span><span class=“n”>function</span><span class=“o”>.</span><span class=“n”>name</span><span class=“p”>,</span> <span class=“n”>text</span><span class=“p”>)</span> <span class=“n”>rst</span> <span class=“o”>=</span> <span class=“n”>function</span><span class=“p”>(</span><span class=“o”>*</span><span class=“n”>args</span><span class=“p”>,</span> <span class=“o”></span><span class=“n”>kwargs</span><span class=“p”>)</span> <span class=“k”>print</span> <span class=“s”>‘after function [</span><span class=“si”>%s</span><span class=“s”>()] run, text: [</span><span class=“si”>%s</span><span class=“s”>].’</span> <span class=“o”>%</span> <span class=“p”>(</span><span class=“n”>function</span><span class=“o”>.</span><span class=“n”>name</span><span class=“p”>,</span> <span class=“n”>text</span><span class=“p”>)</span> <span class=“k”>return</span> <span class=“n”>rst</span> <span class=“k”>return</span> <span class=“n”>wrapper</span> <span class=“k”>return</span> <span class=“n”>decorator</span> <span class=“k”>def</span> <span class=“nf”>wrapper</span><span class=“p”>(</span><span class=“o”>*</span><span class=“n”>args</span><span class=“p”>,</span> <span class=“o”></span><span class=“n”>kwargs</span><span class=“p”>):</span> <span class=“k”>print</span> <span class=“s”>‘before function [</span><span class=“si”>%s</span><span class=“s”>()] run.’</span> <span class=“o”>%</span> <span class=“n”>function</span><span class=“o”>.</span><span class=“n”>name</span> <span class=“n”>rst</span> <span class=“o”>=</span> <span class=“n”>function</span><span class=“p”>(</span><span class=“o”>*</span><span class=“n”>args</span><span class=“p”>,</span> <span class=“o”></span><span class=“n”>kwargs</span><span class=“p”>)</span> <span class=“k”>print</span> <span class=“s”>‘after function [</span><span class=“si”>%s</span><span class=“s”>()] run.’</span> <span class=“o”>%</span> <span class=“n”>function</span><span class=“o”>.</span><span class=“n”>name</span> <span class=“k”>return</span> <span class=“n”>rst</span> <span class=“k”>return</span> <span class=“n”>wrapper</span>

如上~~~