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就是一个返回函数的高阶函数

log函数,参数text

decorator 参数为func函数

wrapper函数 打印text和func函数名

返回func函数

返回wrapper函数

返回decorator函数

埋日志

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

执行函数

now()

结果:execute now():

now

原文地址:https://www.cnblogs.com/cutepython/p/5996875.html