Python闭包的高级应用-装饰器的实现

本文介绍了Python中装饰器的概念及其实现原理,通过具体的代码示例解释了如何使用闭包来实现装饰器,包括基本装饰器的功能以及扩展装饰器功能的方法。

我们先看一个闭包的例子:

from time import ctime

def before_call(f):
    def wrapped(*args, **kargs):
        print 'before calling, now is %s' % ctime()
        return f(*args, **kargs)
    return wrapped

def test(name):
    print 'hello, %s' % (name)

if __name__ == '__main__':
    before_call(test)("lucky")

我们先看运行结果:

~/Documents/py python 2.py 
before calling, now is Sat Dec 27 21:30:18 2014
hello, lucky

上面的代码使用了闭包,因为子函数wrapped将父函数的内部变量f与之绑定。

这样,wrapped这个闭包函数,实际上先打印时间,然后调用f,所以正如结果打印的一般,before_call起到的是一种装饰的作用

 

这里我扩展它的功能,增加一个调用函数后,打印时间:

from time import ctime

def before_call(f):
    def wrapped(*args, **kargs):
        print 'before calling, now is %s' % ctime()
        return f(*args, **kargs)
    return wrapped

def after_call(f):
    def wrapped(*args, **kargs):
        try:
            return f(*args, **kargs)
        finally:
            print 'after calling, now is %s' % ctime()
    return wrapped

def test(name):
    print 'hello, %s' % (name)

if __name__ == '__main__':
    before_call(test)("lucky")
    after_call(test)("peter")
    before_call(after_call(test))("john")
    after_call(before_call(test))('marry')

运行结果为:

~/Documents/py python 2.py 
before calling, now is Sat Dec 27 21:37:24 2014
hello, lucky
hello, peter
after calling, now is Sat Dec 27 21:37:24 2014
before calling, now is Sat Dec 27 21:37:24 2014
hello, john
after calling, now is Sat Dec 27 21:37:24 2014
before calling, now is Sat Dec 27 21:37:24 2014
hello, marry
after calling, now is Sat Dec 27 21:37:24 2014

运行结果是正确的。注意最后两个,顺序交换了,对结果无影响。

 

下面我们再包装一层:

def after_call():
    def after(f):
        def wrapped(*args, **kargs):
            try:
                return f(*args, **kargs)
            finally:
                print 'after calling, now is %s' % ctime()
        return wrapped
    return after


def before_call():
    def before(f):
        def wrapped(*args, **kargs):
            print 'before calling, now is %s' % ctime()
            return f(*args, **kargs)
        return wrapped
    return before

那么如何使用呢?这里就是python装饰器的语法,

如果我们这样使用:

@before_call()
def test(name):
    print 'hello, %s' % (name)

if __name__ == '__main__':
    test("lucky")

注意test函数前加了装饰的符号。

还可以这样:

@after_call()
def test(name):
    print 'hello, %s' % (name)

甚至可以嵌套多层:

@before_call()
@after_call()
def test(name):
    print 'hello, %s' % (name)

 

这就是python中装饰器的原理,内部采用了闭包。

转载于:https://www.cnblogs.com/inevermore/p/4189225.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值