Currying decorator in python

前端 未结 9 1794
故里飘歌
故里飘歌 2020-12-03 03:11

I am trying to write a currying decorator in python, and I think I\'ve got the general idea down, but still got some cases that aren\'t working right...

def          


        
9条回答
  •  渐次进展
    2020-12-03 04:01

    The solution from Roger Christman will not work with every constellation. I applied a small fix to also handle this situation:

    curried_func(1)(2,3)
    

    The small fix that makes it work with every constellation lies in the returned lambda:

    def curried(func):
        def curry(*args):
            if len(args) == func.__code__.co_argcount:
                ans = func(*args)
                return ans
            else:
                return lambda *x: curry(*(args+x))
        return curry
    

提交回复
热议问题