Currying decorator in python

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

    This one is fairly simple and doesn't use inspect or examine the given function's args

    import functools
    
    
    def curried(func):
        """A decorator that curries the given function.
    
        @curried
        def a(b, c):
            return (b, c)
    
        a(c=1)(2)  # returns (2, 1)
        """
        @functools.wraps(func)
        def _curried(*args, **kwargs):
            return functools.partial(func, *args, **kwargs)
        return _curried
    

提交回复
热议问题