Python feature request : operator for function composition

Arnaud Delobelle arnodel at googlemail.com
Sun Feb 3 04:55:19 EST 2008


On Feb 3, 9:43 am, Kay Schluehr <kay.schlu... at gmx.net> wrote:
> On 3 Feb., 10:13, Arnaud Delobelle <arno... at googlemail.com> wrote:
>
>
>
> > 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
>
> Supporting general callables would be very fine. I thing a callable
> ABC with two protocols named __call__ and __compose__ would be most
> adequate. I'm just not sure if Python 2.X requires a more ad hoc
> implementation for builtin callable types? On the level of user
> defined classes a bundling between __call__ and __compose__ shall
> remain optional ( unlike e.g. the dependency between __iter__ and
> __next__ for iterables ).

This would conflict with the fact that the operators you suggest for
composition can already be overloaded for other purposes.

--
Arnaud




More information about the Python-list mailing list