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
原文地址:https://www.cnblogs.com/backinfile/p/5953726.html