我试图用Python语言编写一个钻营装饰,我想我已经得到了普遍的想法了,但还是有一些情况下,那些不工作的权利......柯里装饰在Python
def curry(fun):
cache = []
numargs = fun.func_code.co_argcount
def new_fun(*args, **kwargs):
print args
print kwargs
cache.extend(list(args))
if len(cache) >= numargs: # easier to do it explicitly than with exceptions
temp = []
for _ in xrange(numargs):
temp.append(cache.pop())
fun(*temp)
return new_fun
@curry
def myfun(a,b):
print a,b
在以下情况下能正常工作:
myfun(5)
myfun(5)
对于失败下面的情况:
myfun(6)(7)
如何任何指针正确地做到这一点将不胜感激!
谢谢!
为什么不在functools模块中使用类似partial的东西? [链接](http://docs.python.org/library/functools.html#functools.partial) – astevanovic 2012-02-26 23:48:26
@digivampire:因为它不会做currying,可能。 – 2012-02-27 00:00:19
写一个装饰器用于什么目的? – 2012-02-27 08:01:27