<p></p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Arial; font-size:14px; line-height:26px"> 前段时间在看一些有关python web framwork的时候,发现在python语言里竟然有“@”符号,一查资料,原来是python装饰器(python装饰器也可以通过除了“@”的其他语法进行定义)。装饰器,是一种设计模式,用于动态地给对象添加行为,之前的一篇<a href="http://qxavier.info/2011/04/08/decorator_pattern/" rel="noopener noreferrer" style="color:rgb(202,0,0); text-decoration:none" target="_blank" title="Decorator模式初探">文章</a>也提到过。python中也有装饰器,不过和普遍意义上的装饰器不同,python中的装饰器实际上是一种“<a href="http://en.wikipedia.org/wiki/Syntactic_sugar" rel="noopener noreferrer" style="color:rgb(202,0,0); text-decoration:none" target="_blank">语法糖</a>”,是一种语句的简便写法。比如,a[idx]就是*(a+idx)的一种简便写法,也算是一种语法糖。 假设有如下写法:</p>
<div style="font-family:Arial; font-size:14px; line-height:26px">
<div class="syntaxhighlighter py" id="highlighter_639209">
<table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td class="gutter">
<div class="line number1 index0 alt2">
1
</div>
<div class="line number2 index1 alt1">
2
</div>
<div class="line number3 index2 alt2">
3
</div> </td><td class="code">
<div class="container">
<div class="line number1 index0 alt2">
<code class="py decorator">@dec</code>
</div>
<div class="line number2 index1 alt1">
<code class="py keyword">def</code>
<code class="py plain">func():</code>
</div>
<div class="line number3 index2 alt2">
<code class="py spaces"> </code>
<code class="py keyword">pass</code>
</div>
</div> </td></tr></tbody></table>
</div>
</div>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Arial; font-size:14px; line-height:26px"> 它等同于:</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Arial; font-size:14px; line-height:26px"> </p>
<div style="font-family:Arial; font-size:14px; line-height:26px">
<div class="syntaxhighlighter py" id="highlighter_392468">
<table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td class="gutter">
<div class="line number1 index0 alt2">
1
</div> </td><td class="code">
<div class="container">
<div class="line number1 index0 alt2">
<code class="py plain">func </code>
<code class="py keyword">=</code>
<code class="py plain">dec(func)</code>
</div>
</div> </td></tr></tbody></table>
</div>
</div>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Arial; font-size:14px; line-height:26px"> dec也是一个函数,只不过这个函数比较特殊,它的参数是一个函数(就是原先被装饰的函数func),它的返回值是一个函数。这样,再运行func()时,就是运行经过“装饰”的函数了。 python decorator可以帮助我们轻松地为函数或者类添加行为,而不用像普通的装饰器模式那样基于某个接口,大概这也算是动态语言的优势之一吧。 好,来看一些例子。</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Arial; font-size:14px; line-height:26px"> <strong>面向切面的编程</strong></p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Arial; font-size:14px; line-height:26px"> <strong></strong>当我们要对许多函数进行相同的测试或者进行其他处理的时候,如果在每个函数都写一遍的话,代码太过重复,不利于统一管理。我们可以写一些统一的函数,然后在需要进行处理的函数前面加上decorator就行了。</p>
<div style="font-family:Arial; font-size:14px; line-height:26px">
<div class="syntaxhighlighter py" id="highlighter_688688">
<table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td class="gutter">
<div class="line number1 index0 alt2">
1
</div>
<div class="line number2 index1 alt1">
2
</div>
<div class="line number3 index2 alt2">
3
</div>
<div class="line number4 index3 alt1">
4
</div>
<div class="line number5 index4 alt2">
5
</div>
<div class="line number6 index5 alt1">
6
</div>
<div class="line number7 index6 alt2">
7
</div>
<div class="line number8 index7 alt1">
8
</div>
<div class="line number9 index8 alt2">
9
</div>
<div class="line number10 index9 alt1">
10
</div>
<div class="line number11 index10 alt2">
11
</div>
<div class="line number12 index11 alt1">
12
</div>
<div class="line number13 index12 alt2">
13
</div>
<div class="line number14 index13 alt1">
14
|
|