python 装饰器

来源:互联网 发布:string数组去掉空格 编辑:程序博客网 时间:2024/05/07 00:59
发布于 2014-10-12收起评论感谢
分享
 

收藏没有帮助举报 作者保留权利

作者:xlzd
链接:http://www.zhihu.com/question/26930016/answer/81263287
来源:知乎
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

简单来讲,可以不严谨地把Python的装饰器看做一个包装函数的函数。

比如,有一个函数:

def func():    print 'func() run.'if '__main__' == __name__:    func()

运行后将输出:

func() run.

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

def log(function):    def wrapper(*args, **kwargs):        print 'before function [%s()] run.' % function.__name__        rst = function(*args, **kwargs)        print 'after function [%s()] run.' % function.__name__        return rst    return wrapper @logdef func():    print 'func() run.'if '__main__' == __name__:    func()

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

before function [func()] run.func() run.after function [func()] run.

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

func = log(func)

因为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!'

实现如下:

import functoolsdef log(text=''):    def decorator(function):        @functools.wraps(function)        def wrapper(*args, **kwargs):            print 'before function [%s()] run, text: [%s].' % (function.__name__, text)            rst = function(*args, **kwargs)            print 'after function [%s()] run, text: [%s].' % (function.__name__, text)            return rst         return wrapper    return decorator@log('log text')def func():    print 'func() run.'if '__main__' == __name__:    func()

输出如下:

before function [func()] run, text: [log text].func() run.after function [func()] run, text: [log text].

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

import functoolsdef log(argument):    if not callable(argument):        def decorator(function):            @functools.wraps(function)            def wrapper(*args, **kwargs):                print 'before function [%s()] run, text: [%s].' % (function.__name__, text)                rst = function(*args, **kwargs)                print 'after function [%s()] run, text: [%s].' % (function.__name__, text)                return rst             return wrapper        return decorator    def wrapper(*args, **kwargs):        print 'before function [%s()] run.' % function.__name__        rst = argument(*args, **kwargs)        print 'after function [%s()] run.' % function.__name__        return rst    return wrapper

如上~~~




这是在Python学习小组上介绍的内容,现学现卖、多练习是好的学习方式。

第一步:最简单的函数,准备附加额外功能

1
2
3
4
5
6
7
8
# -*- coding:gbk -*-
'''示例1: 最简单的函数,表示调用了两次'''
 
def myfunc():
    print("myfunc() called.")
 
myfunc()
myfunc()

 

第二步:使用装饰函数在函数执行前和执行后分别附加额外功能

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# -*- coding:gbk -*-
'''示例2: 替换函数(装饰)
装饰函数的参数是被装饰的函数对象,返回原函数对象
装饰的实质语句: myfunc = deco(myfunc)'''
 
def deco(func):
    print("before myfunc() called.")
    func()
    print("  after myfunc() called.")
    return func
 
def myfunc():
    print(" myfunc() called.")
 
myfunc= deco(myfunc)
 
myfunc()
myfunc()

第三步:使用语法糖@来装饰函数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# -*- coding:gbk -*-
'''示例3: 使用语法糖@来装饰函数,相当于“myfunc = deco(myfunc)”
但发现新函数只在第一次被调用,且原函数多调用了一次'''
 
def deco(func):
    print("before myfunc() called.")
    func()
    print("  after myfunc() called.")
    return func
 
@deco
def myfunc():
    print(" myfunc() called.")
 
myfunc()
myfunc()

第四步:使用内嵌包装函数来确保每次新函数都被调用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# -*- coding:gbk -*-
'''示例4: 使用内嵌包装函数来确保每次新函数都被调用,
内嵌包装函数的形参和返回值与原函数相同,装饰函数返回内嵌包装函数对象'''
 
def deco(func):
    def _deco():
        print("before myfunc() called.")
        func()
        print("  after myfunc() called.")
        # 不需要返回func,实际上应返回原函数的返回值
    return _deco
 
@deco
def myfunc():
    print(" myfunc() called.")
    return 'ok'
 
myfunc()
myfunc()

第五步:对带参数的函数进行装饰

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# -*- coding:gbk -*-
'''示例5: 对带参数的函数进行装饰,
内嵌包装函数的形参和返回值与原函数相同,装饰函数返回内嵌包装函数对象'''
 
def deco(func):
    def _deco(a, b):
        print("before myfunc() called.")
        ret= func(a, b)
        print("  after myfunc() called. result: %s" % ret)
        return ret
    return _deco
 
@deco
def myfunc(a, b):
    print(" myfunc(%s,%s) called." % (a, b))
    return a+ b
 
myfunc(1,2)
myfunc(3,4)

第六步:对参数数量不确定的函数进行装饰

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
# -*- coding:gbk -*-
'''示例6: 对参数数量不确定的函数进行装饰,
参数用(*args, **kwargs),自动适应变参和命名参数'''
 
def deco(func):
    def _deco(*args,**kwargs):
        print("before %s called." % func.__name__)
        ret= func(*args,**kwargs)
        print("  after %s called. result: %s" % (func.__name__, ret))
        return ret
    return _deco
 
@deco
def myfunc(a, b):
    print(" myfunc(%s,%s) called." % (a, b))
    return a+b
 
@deco
def myfunc2(a, b, c):
    print(" myfunc2(%s,%s,%s) called." % (a, b, c))
    return a+b+c
 
myfunc(1,2)
myfunc(3,4)
myfunc2(1,2,3)
myfunc2(3,4,5)

第七步:让装饰器带参数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# -*- coding:gbk -*-
'''示例7: 在示例4的基础上,让装饰器带参数,
和上一示例相比在外层多了一层包装。
装饰函数名实际上应更有意义些'''
 
def deco(arg):
    def _deco(func):
        def __deco():
            print("before %s called [%s]." % (func.__name__, arg))
            func()
            print("  after %s called [%s]." % (func.__name__, arg))
        return __deco
    return _deco
 
@deco("mymodule")
def myfunc():
    print(" myfunc() called.")
 
@deco("module2")
def myfunc2():
    print(" myfunc2() called.")
 
myfunc()
myfunc2()

第八步:让装饰器带 类 参数

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
# -*- coding:gbk -*-
'''示例8: 装饰器带类参数'''
 
class locker:
    def __init__(self):
        print("locker.__init__() should be not called.")
         
    @staticmethod
    def acquire():
        print("locker.acquire() called.(这是静态方法)")
         
    @staticmethod
    def release():
        print("  locker.release() called.(不需要对象实例)")
 
def deco(cls):
    '''cls 必须实现acquire和release静态方法'''
    def _deco(func):
        def __deco():
            print("before %s called [%s]." % (func.__name__,cls))
            cls.acquire()
            try:
                return func()
            finally:
                cls.release()
        return __deco
    return _deco
 
@deco(locker)
def myfunc():
    print(" myfunc() called.")
 
myfunc()
myfunc()

第九步:装饰器带类参数,并分拆公共类到其他py文件中,同时演示了对一个函数应用多个装饰器

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
36
# -*- coding:gbk -*-
'''mylocker.py: 公共类 for 示例9.py'''
 
class mylocker:
    def __init__(self):
        print("mylocker.__init__() called.")
         
    @staticmethod
    def acquire():
        print("mylocker.acquire() called.")
         
    @staticmethod
    def unlock():
        print("  mylocker.unlock() called.")
 
class lockerex(mylocker):
    @staticmethod
    def acquire():
        print("lockerex.acquire() called.")
         
    @staticmethod
    def unlock():
        print("  lockerex.unlock() called.")
 
def lockhelper(cls):
    '''cls 必须实现acquire和release静态方法'''
    def _deco(func):
        def __deco(*args,**kwargs):
            print("before %s called." % func.__name__)
            cls.acquire()
            try:
                return func(*args,**kwargs)
            finally:
                cls.unlock()
        return __deco
    return _deco

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# -*- coding:gbk -*-
'''示例9: 装饰器带类参数,并分拆公共类到其他py文件中
同时演示了对一个函数应用多个装饰器'''
 
from mylockerimport *
 
class example:
    @lockhelper(mylocker)
    def myfunc(self):
        print(" myfunc() called.")
 
    @lockhelper(mylocker)
    @lockhelper(lockerex)
    def myfunc2(self, a, b):
        print(" myfunc2() called.")
        return a+ b
 
if __name__=="__main__":
    a= example()
    a.myfunc()
    print(a.myfunc())
    print(a.myfunc2(1,2))
    print(a.myfunc2(3,4))

最后一个函数分析:

def lockhelper(cls):
    '''cls 必须实现acquire和release静态方法'''
    def _deco(func):
        def __deco(*args,**kwargs):
            print("before %s called." % func.__name__)
            cls.acquire()
            try:
                return func(*args,**kwargs)
            finally:
                cls.unlock()
        return __deco
    return _deco
这个函数使用修饰的func变量,故,对于

    @lockhelper(mylocker)
    @lockhelper(lockerex)
    def myfunc2(self, a, b):
        print(" myfunc2() called.")
        return a+ b
先由
    @lockhelper(lockerex)
    def myfunc2(self, a, b):
        print(" myfunc2() called.")
        return a+ b
第一步完成myfunc2的修饰,返回__deco,并将__deco与lockerex参数打包在一起。然后,__deco函数接受@lockhelper(mylocker)的修饰,并和mylocker打包在一起,所以这个环境共有三层:
1.最内层:myfun函数及其变量a+b;
2.中层:__deco函数及其变量lockerex
3.外层:mylocker变量
所以运行结果为:
before __deco called.
mylocker.acquire() called.
before myfunc2() called.
locerex.acquire() called.
myfunc2() called.
lockerex.unlock() called.
mylocker.unlock() called.

结果分层来看表示如下(以【】区分):
【【【before __deco called.
mylocker.acquire() called.
【【before myfunc2() called.
locerex.acquire() called.
【myfunc2() called.】
lockerex.unlock() called.】】
mylocker.unlock() called.】】】


下面是参考资料,当初有不少地方没看明白,真正练习后才明白些:

1. Python装饰器学习 http://blog.csdn.net/thy38/article/details/4471421

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

3. Python装饰器的理解 http://apps.hi.baidu.com/share/detail/17572338


0 0
原创粉丝点击