码迷,mamicode.com
首页 > 编程语言 > 详细

Python装饰函数

时间:2017-12-29 21:44:00      阅读:128      评论:0      收藏:0      [点我收藏+]

标签:import   fun   内部函数   gpo   app   python   ctime   __name__   from   

 

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()以后,

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

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

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

Python装饰函数

标签:import   fun   内部函数   gpo   app   python   ctime   __name__   from   

原文地址:https://www.cnblogs.com/Sabre/p/8146285.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!