Currying decorator in python

前端 未结 9 1792
故里飘歌
故里飘歌 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:05

    The below implementation is naive, google for "currying python" for more accurate examples.

    def curry(x, argc=None):
        if argc is None:
            argc = x.func_code.co_argcount
        def p(*a):
            if len(a) == argc:
                return x(*a)
            def q(*b):
                return x(*(a + b))
            return curry(q, argc - len(a))
        return p
    
    @curry
    def myfun(a,b,c):
        print '%d-%d-%d' % (a,b,c)
    
    
    
    myfun(11,22,33)
    myfun(44,55)(66)
    myfun(77)(88)(99)
    

提交回复
热议问题