【转】理解Python中的装饰器

本文详细介绍了Python装饰器的概念和工作原理,通过多个示例展示了如何使用装饰器来增强函数的功能,而不修改其原有代码。

前几天看到Stackoverflow上的一个提问。如果使用如下的代码:

?
1
2
3
4
@makebold
@makeitalic
def say():
    return "Hello"

打印出如下的输出:

?
1
<b><i>Hello<i></b>

给出的答案如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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>

想要做更多深入的了解python的装饰器。你可以参考两篇很好的中文博客文章:

1. Python装饰器学习:http://blog.youkuaiyun.com/thy38/archive/2009/08/21/4471421.aspx

2. Python装饰器与面向切面编程:http://www.cnblogs.com/huxi/archive/2011/03/01/1967600.html

现在我们来看看如何从一些最基础的方式来理解Python的装饰器。英文讨论参考Here

要理解python的装饰器,我们首先必须明白在Python中函数也是被视为对象。这一点很重要。先看一个例子:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
def shout ( word = "yes" ) :
     return word .capitalize ( ) + " !"
 
print shout ( )
# 输出 : 'Yes !'
 
# 作为一个对象,你可以把函数赋给任何其他对象变量
 
scream = shout
 
# 注意我们没有使用圆括号,因为我们不是在调用函数
# 我们把函数shout赋给scream,也就是说你可以通过scream调用shout
 
print scream ( )
# 输出 : 'Yes !'
 
# 还有,你可以删除旧的名字shout,但是你仍然可以通过scream来访问该函数
 
del shout
try :
     print shout ( )
except NameError , e :
     print e
     #输出 : "name 'shout' is not defined"
 
print scream ( )
# 输出 : 'Yes !'

我们暂且把这个话题放旁边,我们先看看python另外一个很有意思的属性:可以在函数中定义函数:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
def talk ( ) :
 
     # 你可以在talk中定义另外一个函数
     def whisper ( word = "yes" ) :
         return word .lower ( ) + "..." ;
 
     # ... 并且立马使用它
 
     print whisper ( )
 
# 你每次调用'talk',定义在talk里面的whisper同样也会被调用
talk ( )
# 输出 :
# yes...
 
# 但是"whisper" 不会单独存在:
 
try :
     print whisper ( )
except NameError , e :
     print e
     #输出 : "name 'whisper' is not defined"*

函数引用

从以上两个例子我们可以得出,函数既然作为一个对象,因此:

1. 其可以被赋给其他变量

2. 其可以被定义在另外一个函数内

这也就是说,函数可以返回一个函数,看下面的例子:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
def getTalk ( type = "shout" ) :
 
     # 我们定义另外一个函数
     def shout ( word = "yes" ) :
         return word .capitalize ( ) + " !"
 
     def whisper ( word = "yes" ) :
         return word .lower ( ) + "..." ;
 
     # 然后我们返回其中一个
     if type = = "shout" :
         # 我们没有使用(),因为我们不是在调用该函数
         # 我们是在返回该函数
         return shout
     else :
         return whisper
 
# 然后怎么使用呢 ?
 
# 把该函数赋予某个变量
talk = getTalk ( )     
 
# 这里你可以看到talk其实是一个函数对象:
print talk
#输出 : <function shout at 0xb7ea817c>
 
# 该对象由函数返回的其中一个对象:
print talk ( )
 
# 或者你可以直接如下调用 :
print getTalk ( "whisper" ) ( )
#输出 : yes...

还有,既然可以返回一个函数,我们可以把它作为参数传递给函数:

?
1
2
3
4
5
6
7
8
def doSomethingBefore(func) :
     print "I do something before then I call the function you gave me"
     print func()
 
doSomethingBefore(scream)
#输出 :
#I do something before then I call the function you gave me
#Yes !

这里你已经足够能理解装饰器了,其他它可被视为封装器。也就是说,它能够让你在装饰前后执行代码而无须改变函数本身内容。

手工装饰

那么如何进行手动装饰呢?

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# 装饰器是一个函数,而其参数为另外一个函数
def my_shiny_new_decorator(a_function_to_decorate) :
 
     # 在内部定义了另外一个函数:一个封装器。
     # 这个函数将原始函数进行封装,所以你可以在它之前或者之后执行一些代码
     def the_wrapper_around_the_original_function() :
 
         # 放一些你希望在真正函数执行前的一些代码
         print "Before the function runs"
 
         # 执行原始函数
         a_function_to_decorate()
 
         # 放一些你希望在原始函数执行后的一些代码
         print "After the function runs"
 
     #在此刻,"a_function_to_decrorate"还没有被执行,我们返回了创建的封装函数
     #封装器包含了函数以及其前后执行的代码,其已经准备完毕
     return the_wrapper_around_the_original_function
 
# 现在想象下,你创建了一个你永远也不远再次接触的函数
def a_stand_alone_function() :
     print "I am a stand alone function, don't you dare modify me"
 
a_stand_alone_function()
#输出: I am a stand alone function, don't you dare modify me
 
# 好了,你可以封装它实现行为的扩展。可以简单的把它丢给装饰器
# 装饰器将动态地把它和你要的代码封装起来,并且返回一个新的可用的函数。
a_stand_alone_function_decorated = my_shiny_new_decorator(a_stand_alone_function)
a_stand_alone_function_decorated()
#输出 :
#Before the function runs
#I am a stand alone function, don't you dare modify me
#After the function runs

现在你也许要求当每次调用a_stand_alone_function时,实际调用却是 a_stand_alone_function_decorated。实现也很简单,可以用my_shiny_new_decorator来给 a_stand_alone_function重新赋值。

?
1
2
3
4
5
6
7
8
a_stand_alone_function = my_shiny_new_decorator(a_stand_alone_function)
a_stand_alone_function()
#输出 :
#Before the function runs
#I am a stand alone function, don't you dare modify me
#After the function runs
 
# And guess what, that's EXACTLY what decorators do !

装饰器揭秘

前面的例子,我们可以使用装饰器的语法:

?
1
2
3
4
5
6
7
8
9
@my_shiny_new_decorator
def another_stand_alone_function ( ) :
     print "Leave me alone"
 
another_stand_alone_function ( )
#输出 :
#Before the function runs
#Leave me alone
#After the function runs

是的,就是这么简单。@decorator可以表示为:

?
1
another_stand_alone_function = my_shiny_new_decorator(another_stand_alone_function)

装饰器仅仅是装饰器设计模型的一个python话变体。python中还有其他经典的设计模式来简化开发,比如Iterator。

当然你也可以累积装饰:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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()
#outputs :
#</''''''\>
# #tomatoes#
# --ham--
# ~salad~
#<\______/>

使用python装饰器语法:

?
1
2
3
4
5
6
7
8
9
10
11
12
@bread
@ingredients
def sandwich(food = "--ham--" ) :
     print food
 
sandwich()
#输出 :
#</''''''\>
# #tomatoes#
# --ham--
# ~salad~
#<\______/>

装饰器的顺序很重要,需要注意:

?
1
2
3
4
5
6
7
8
9
10
11
12
@ingredients
@bread
def strange_sandwich(food = "--ham--" ) :
     print food
 
strange_sandwich()
#输出 :
##tomatoes#
#</''''''\>
# --ham--
#<\______/>
# ~salad~

最后回答前面提到的问题:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# 装饰器makebold用于转换为粗体
def makebold(fn):
     # 结果返回该函数
     def wrapper():
         # 插入一些执行前后的代码
         return "<b>" + fn() + "</b>"
     return wrapper
 
# 装饰器makeitalic用于转换为斜体
def makeitalic(fn):
     # 结果返回该函数
     def wrapper():
         # 插入一些执行前后的代码
         return "<i>" + fn() + "</i>"
     return wrapper
 
@makebold
@makeitalic
def say():
     return "hello"
 
print say()
#输出: <b><i>hello</i></b>
 
# 等同于
def say():
     return "hello"
say = makebold(makeitalic(say))
 
print say()
#输出: <b><i>hello</i></b>

前几天看到Stackoverflow上的一个提问。如果使用如下的代码:

?
1
2
3
4
@makebold
@makeitalic
def say():
    return "Hello"

打印出如下的输出:

?
1
<b><i>Hello<i></b>

给出的答案如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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>

想要做更多深入的了解python的装饰器。你可以参考两篇很好的中文博客文章:

1. Python装饰器学习:http://blog.youkuaiyun.com/thy38/archive/2009/08/21/4471421.aspx

2. Python装饰器与面向切面编程:http://www.cnblogs.com/huxi/archive/2011/03/01/1967600.html

现在我们来看看如何从一些最基础的方式来理解Python的装饰器。英文讨论参考Here

要理解python的装饰器,我们首先必须明白在Python中函数也是被视为对象。这一点很重要。先看一个例子:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
def shout ( word = "yes" ) :
     return word .capitalize ( ) + " !"
 
print shout ( )
# 输出 : 'Yes !'
 
# 作为一个对象,你可以把函数赋给任何其他对象变量
 
scream = shout
 
# 注意我们没有使用圆括号,因为我们不是在调用函数
# 我们把函数shout赋给scream,也就是说你可以通过scream调用shout
 
print scream ( )
# 输出 : 'Yes !'
 
# 还有,你可以删除旧的名字shout,但是你仍然可以通过scream来访问该函数
 
del shout
try :
     print shout ( )
except NameError , e :
     print e
     #输出 : "name 'shout' is not defined"
 
print scream ( )
# 输出 : 'Yes !'

我们暂且把这个话题放旁边,我们先看看python另外一个很有意思的属性:可以在函数中定义函数:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
def talk ( ) :
 
     # 你可以在talk中定义另外一个函数
     def whisper ( word = "yes" ) :
         return word .lower ( ) + "..." ;
 
     # ... 并且立马使用它
 
     print whisper ( )
 
# 你每次调用'talk',定义在talk里面的whisper同样也会被调用
talk ( )
# 输出 :
# yes...
 
# 但是"whisper" 不会单独存在:
 
try :
     print whisper ( )
except NameError , e :
     print e
     #输出 : "name 'whisper' is not defined"*

函数引用

从以上两个例子我们可以得出,函数既然作为一个对象,因此:

1. 其可以被赋给其他变量

2. 其可以被定义在另外一个函数内

这也就是说,函数可以返回一个函数,看下面的例子:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
def getTalk ( type = "shout" ) :
 
     # 我们定义另外一个函数
     def shout ( word = "yes" ) :
         return word .capitalize ( ) + " !"
 
     def whisper ( word = "yes" ) :
         return word .lower ( ) + "..." ;
 
     # 然后我们返回其中一个
     if type = = "shout" :
         # 我们没有使用(),因为我们不是在调用该函数
         # 我们是在返回该函数
         return shout
     else :
         return whisper
 
# 然后怎么使用呢 ?
 
# 把该函数赋予某个变量
talk = getTalk ( )     
 
# 这里你可以看到talk其实是一个函数对象:
print talk
#输出 : <function shout at 0xb7ea817c>
 
# 该对象由函数返回的其中一个对象:
print talk ( )
 
# 或者你可以直接如下调用 :
print getTalk ( "whisper" ) ( )
#输出 : yes...

还有,既然可以返回一个函数,我们可以把它作为参数传递给函数:

?
1
2
3
4
5
6
7
8
def doSomethingBefore(func) :
     print "I do something before then I call the function you gave me"
     print func()
 
doSomethingBefore(scream)
#输出 :
#I do something before then I call the function you gave me
#Yes !

这里你已经足够能理解装饰器了,其他它可被视为封装器。也就是说,它能够让你在装饰前后执行代码而无须改变函数本身内容。

手工装饰

那么如何进行手动装饰呢?

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# 装饰器是一个函数,而其参数为另外一个函数
def my_shiny_new_decorator(a_function_to_decorate) :
 
     # 在内部定义了另外一个函数:一个封装器。
     # 这个函数将原始函数进行封装,所以你可以在它之前或者之后执行一些代码
     def the_wrapper_around_the_original_function() :
 
         # 放一些你希望在真正函数执行前的一些代码
         print "Before the function runs"
 
         # 执行原始函数
         a_function_to_decorate()
 
         # 放一些你希望在原始函数执行后的一些代码
         print "After the function runs"
 
     #在此刻,"a_function_to_decrorate"还没有被执行,我们返回了创建的封装函数
     #封装器包含了函数以及其前后执行的代码,其已经准备完毕
     return the_wrapper_around_the_original_function
 
# 现在想象下,你创建了一个你永远也不远再次接触的函数
def a_stand_alone_function() :
     print "I am a stand alone function, don't you dare modify me"
 
a_stand_alone_function()
#输出: I am a stand alone function, don't you dare modify me
 
# 好了,你可以封装它实现行为的扩展。可以简单的把它丢给装饰器
# 装饰器将动态地把它和你要的代码封装起来,并且返回一个新的可用的函数。
a_stand_alone_function_decorated = my_shiny_new_decorator(a_stand_alone_function)
a_stand_alone_function_decorated()
#输出 :
#Before the function runs
#I am a stand alone function, don't you dare modify me
#After the function runs

现在你也许要求当每次调用a_stand_alone_function时,实际调用却是 a_stand_alone_function_decorated。实现也很简单,可以用my_shiny_new_decorator来给 a_stand_alone_function重新赋值。

?
1
2
3
4
5
6
7
8
a_stand_alone_function = my_shiny_new_decorator(a_stand_alone_function)
a_stand_alone_function()
#输出 :
#Before the function runs
#I am a stand alone function, don't you dare modify me
#After the function runs
 
# And guess what, that's EXACTLY what decorators do !

装饰器揭秘

前面的例子,我们可以使用装饰器的语法:

?
1
2
3
4
5
6
7
8
9
@my_shiny_new_decorator
def another_stand_alone_function ( ) :
     print "Leave me alone"
 
another_stand_alone_function ( )
#输出 :
#Before the function runs
#Leave me alone
#After the function runs

是的,就是这么简单。@decorator可以表示为:

?
1
another_stand_alone_function = my_shiny_new_decorator(another_stand_alone_function)

装饰器仅仅是装饰器设计模型的一个python话变体。python中还有其他经典的设计模式来简化开发,比如Iterator。

当然你也可以累积装饰:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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()
#outputs :
#</''''''\>
# #tomatoes#
# --ham--
# ~salad~
#<\______/>

使用python装饰器语法:

?
1
2
3
4
5
6
7
8
9
10
11
12
@bread
@ingredients
def sandwich(food = "--ham--" ) :
     print food
 
sandwich()
#输出 :
#</''''''\>
# #tomatoes#
# --ham--
# ~salad~
#<\______/>

装饰器的顺序很重要,需要注意:

?
1
2
3
4
5
6
7
8
9
10
11
12
@ingredients
@bread
def strange_sandwich(food = "--ham--" ) :
     print food
 
strange_sandwich()
#输出 :
##tomatoes#
#</''''''\>
# --ham--
#<\______/>
# ~salad~

最后回答前面提到的问题:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# 装饰器makebold用于转换为粗体
def makebold(fn):
     # 结果返回该函数
     def wrapper():
         # 插入一些执行前后的代码
         return "<b>" + fn() + "</b>"
     return wrapper
 
# 装饰器makeitalic用于转换为斜体
def makeitalic(fn):
     # 结果返回该函数
     def wrapper():
         # 插入一些执行前后的代码
         return "<i>" + fn() + "</i>"
     return wrapper
 
@makebold
@makeitalic
def say():
     return "hello"
 
print say()
#输出: <b><i>hello</i></b>
 
# 等同于
def say():
     return "hello"
say = makebold(makeitalic(say))
 
print say()
#输出: <b><i>hello</i></b>
原文链接:http://www.codecho.com/understanding-python-decorators/

转载于:https://www.cnblogs.com/hopeworld/archive/2011/08/02/2125096.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值