[Python-Dev] functools.compose to chain functions together

Xavier Morel catch-all at masklinn.net
Fri Aug 14 20:58:22 CEST 2009


On 14 Aug 2009, at 20:39 , Jason R. Coombs wrote:
> I've heard it said that Python is not a functional language, but if  
> that
> were really the case, then functools would not exist. In addition to  
> the
> example described above, I've had multiple occasions where having a  
> general
> purpose function composition function would have simplified the
> implementation by providing a basic functional construct.

It's not like a basic variable-arity composition function is hard to  
implement though, it's basically:

     def compose(*funcs):
         return reduce(lambda f1, f2:
                           lambda v:
                               f1(f2(v)),
                       funcs)

it'll turn compose(a, b, c, d)(value) into a(b(c(d(value))))


More information about the Python-Dev mailing list