python 通用 修饰器

import functools

def log(option):
    def dec(func):
        def swapper(*arg, **karg):
            functools.update_wrapper(swapper, func)
            opt = format(option,"^7")
            print '['+opt+"] enter",func.__name__
            res = func(*arg, **karg)
            print '['+opt+'] exit',func.__name__
            return res
        return swapper
    return dec

 

posted @ 2016-10-12 17:19  backinfile  阅读(266)  评论(0编辑  收藏  举报