相关疑难解决方法(0)

在python的Currying装饰员

我正在尝试在python中写一个currying装饰器,我想我已经有了一般的想法,但仍然有一些不正常的情况......

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
Run Code Online (Sandbox Code Playgroud)

虽然对于以下情况,这可以正常工作:

myfun(5)
myfun(5)
Run Code Online (Sandbox Code Playgroud)

对于以下情况,它失败:

myfun(6)(7)
Run Code Online (Sandbox Code Playgroud)

任何有关如何正确执行此操作的指示将非常感谢!

谢谢!

python decorator currying

18
推荐指数
3
解决办法
5981
查看次数

标签 统计

currying ×1

decorator ×1

python ×1