函数装饰器

有参数和无参数的装饰器

@deco
def foo():
    pass
@decomaker(deco_args)
def foo():
    pass
@deco1(deco_arg)
@deco2
def func():
    pass

举例

#!/usr/bin/env python

from time import ctime, sleep

def tsfunc(func):
    def wrapperFunc():
        print '[%s] %s() called' % (ctime(), func._name_)
        return func()
    return wrapperFunc

@tsfunc
def foo():
    pass
    
foo()
sleep(4)

for i in range(2):
    sleep(1)
    foo()

摘自《Python核心编程》

posted @ 2015-12-19 01:10  dflyingfish  阅读(102)  评论(0)    收藏  举报