[Python-ideas] Addition to operator module: starcaller

Nick Coghlan ncoghlan at gmail.com
Thu Jul 21 22:39:56 EDT 2016


On 22 July 2016 at 02:36, Daniel Spitz <spitz.dan.l at gmail.com> wrote:
> Well, partial + apply seems like the most powerful solution (supports
> **keywords, opens up the possibility of using apply in other ways). Perhaps
> adding apply back to functools rather than as a builtin would evade some of
> the antipatterns it used to encourage (a la reduce). I'm not sure and don't
> have a strong opinion about the original removal of apply.
>
> Alternatively a form of "starcaller" or "unpackcaller" defined roughly as
> partial(apply) could be added to the operator module in the capacity I
> proposed above. The only possible advantage here is aesthetic; it fits
> conceptually with the rest of the module as a callable capturing a
> repeatable operation that is normally only possible with special syntax. It
> also disallows using apply in "non-curried" contexts.

apply was removed primarily for reasons of redundancy rather than
there being anything particularly objectionable about the function
itself, so I wouldn't anticipate any major objections to giving it the
reduce treatment. The answer to "Which approach should I use when?" is
pretty straightforward: "use the builtin syntax in most situations,
but use functools.apply instead if you specifically need a
higher-order function".

Where I've personally seen that need come up is in distributed task
processing where you normally want the actual computation to happen
*somewhere else* (e.g. on another machine, in another process or
thread, or even as a callback within the current thread), but the
mechanism for actually invoking the callback is itself a callback that
accepts a "(callable, args, kwds)" triple. "functools.apply" then
slots in neatly as a lowest common denominator "just call it
immediately" baseline implementation.

There's also an interesting potential synergy with PEP 484 type
annotations when it comes to manipulating higher order functions: at
the moment, there's no way to explicitly type callables that accept
keyword arguments, but you *can* type a callable that accepts a tuple
and a dict with particular contents as positional arguments.

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia


More information about the Python-ideas mailing list