I sing the praises of lambda, my friend and savior!

Alex Martelli aleaxit at yahoo.com
Fri Oct 15 03:04:53 EDT 2004


Jeff Shannon <jeff at ccvcorp.com> wrote:

> currying horses... ;) )  I believe that his solution involved callable
> class instances that store the function to be called and the known 
> parameters as attributes.  That *was* quite a while ago (possibly even
> pre-2.2), though, so I don't know whether it'd still be the most 
> effective recipe... and my memory about it is a bit flakey.

I think the most effective way to make general curries today is by
closures.  E.g., if what you want to curry are the starting parameters:

def curry(function, *curried_params):
    def curried(*free_params):
        return function(*(curried_params+free_params))
    return curried

In 2.4 you can even set curried.func_name=function.func_name ...;-)

For an important special case, currying the first parameter into a
Python-coded function, function.__get__(first_parameter) happens to
work; I confess that sometimes I've coded functions that take a "first
parameter" that is in fact a tuple of quite a few things just to be able
to use this *delightful* approach (not recommended, I've just got a love
affair with currying, and _dream_ of it being a Python "intrinsic"!-).


Alex



More information about the Python-list mailing list