Python feature request : operator for function composition

Arnaud Delobelle arnodel at googlemail.com
Sun Feb 3 04:13:38 EST 2008


On Feb 3, 5:09 am, Kay Schluehr <kay.schlu... at gmx.net> wrote:
> As you know, there is no operator for function composition in Python.
> When you have two functions F and G and  want to express the
> composition F o G you have to create a new closure
>
> lambda *args, **kwd: F (G (*args, **kwd))
>
> or you write a composition in functional style
>
> compose( F, G )
>
> None of these solutions is particular terse.
>
> Proposal
> -------------
> I want to propose overloading the logical __and__ operator i.e. '&'
> for functions s.t. F & G means "compose(F,G)". This suggestion is non-
> conflicting since & is not used as an operator in combination with
> function objects yet: given a function object F and an arbitrary
> object X the combination F & X raises a TypeError when evaluated.
>
> Alternatives
> -----------------
> One could use other unused operators like F << G or F * G to write
> terse function composition expressions. I' m not sure which one is
> best and would use the proposed & if no one presents arguments against
> it.

What about other callable objects?

--
Arnaud




More information about the Python-list mailing list