Python装饰函数

 

from time import ctime, sleep

def tsfunc(func):
    def wrappedFunc():
        print('[%s] %s() classed' % (ctime(),func.__name__))
        print("先执行装饰器")
        return func()
    print("this is tsfunc")
    return wrappedFunc

@tsfunc
def foo():
    print("this is foo")

@tsfunc
def foo2():
    print("this is foo2")




foo();
foo2();

运行结果

this is tsfunc
this is tsfunc
[Fri Dec 29 20:48:34 2017] foo() classed
先执行装饰器
this is foo
[Fri Dec 29 20:48:34 2017] foo2() classed
先执行装饰器
this is foo2
[Finished in 0.2s]

调用foo()以后,

装饰器中的语句先执行了。

然后开始调用内部函数,执行内部函数的语句

然后开始执行被装饰函数的语句

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值