Differences between revisions 2 and 3
Revision 2 as of 2006-09-08 08:11:12
Size: 647
Editor: auguusstt
Comment:
Revision 3 as of 2006-09-08 08:12:59
Size: 679
Editor: auguusstt
Comment:
Deletions are marked like this. Additions are marked like this.
Line 1: Line 1:
参考WeiZhong/DecoratorsInPython24
Line 13: Line 15:
   
Line 17: Line 19:
Line 20: Line 21:
}}}
输出:
Line 21: Line 24:
}}}

输出:

参考WeiZhong/DecoratorsInPython24

#带参数修饰器函数被调用时必须返回一个新的修饰器函数
#参考http://python.cn/pipermail/python-chinese/2006-January/021034.html
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)

argsDecorator (last edited 2009-12-25 07:16:17 by localhost)