参考WeiZhong/DecoratorsInPython24

#带参数修饰器函数被调用时必须返回一个新的修饰器函数
def decoratorArgs(x,y):
    def decorator(f):
        def func(*args,**keywordArgs):
            print 'invoke: %s(%s,%s)' % (f.__name__,x,y)
            result=f(*args,**keywordArgs)
            print 'invoked: %s(%s,%s)' % (f.__name__,x,y)
            return result
        return func
    return decorator

@decoratorArgs(1,2)
def hello(arg):
    print 'hello:',arg
hello('abc')
print

输出:

invoke: hello(1,2)
hello: abc
invoked: hello(1,2)