decorator

def log(text):
    def decorator(func):
        def wrapper(*args,**kw):
            print '%s %s():' %(text,func.__name__)
            return func(*args,**kw)
        return wrapper
    return decorator

decorator就是一个返回函数的高阶函数app

log函数,参数text函数

decorator 参数为func函数spa

wrapper函数 打印text和func函数名日志

返回func函数code

返回wrapper函数blog

返回decorator函数class

埋日志di

@log(‘execute’)
now():
    print ‘now’

执行函数co

now()

结果:execute now():return

now

相关文章
相关标签/搜索