[Python-ideas] Add 'composable' decorator to functools (with @ matrix multiplication)

Andrew Barnert abarnert at yahoo.com
Wed May 6 22:10:10 CEST 2015


On May 6, 2015, at 12:50, Yann Kaiser <kaiser.yann at gmail.com> wrote:
> 
>> On Wed, 6 May 2015 at 08:49 Guido van Rossum <guido at python.org> wrote:
>> I realize this is still python-ideas, but does this really leave functions with multiple arguments completely out of the picture (except as the first stage in the pipeline)?
> 
> To provide some alternative ideas, what I did in sigtools.wrappers.Combination[1] was to replace the first argument with the return value of the previous call while always using the same remaining (positional and keyword) arguments

This is exactly my point about there not being one obvious right answer for dealing with multiple arguments. Among the choices are:

 * Don't allow them.
 * Auto-*-unpack return values into multiple arguments.
 * Compose on the first argument, pass others along the chain.
 * Auto-curry and auto-partial everywhere.
 * Auto-curry and auto-partial and _also_ auto-*-unpack (which I'd never considered, but it sounds like that's what this thread proposes).

They've all got uses. But if you're going to write _the_ compose function, it has to pick one.

Also, keep in mind that "auto-curry and auto-partial everything" can't really mean "everything"--unlike Haskell, Python can't partial operator expressions, and we've still got *args and keyword-only params and **kw, and we've got C functions that aren't argclinic'd yet, and so on. (If that all seems like obscure edge cases, consider that most proxy implementations, forwarding functions, etc. work by just taking and passing along *args, **kw, not by inspecting and binding the signature of the wrappee.)

> . In code:
> 
>     def __call__(self, arg, *args, **kwargs):
>         for function in self.functions:
>             arg = function(arg, *args, **kwargs)
>         return arg
> 
> With this you can even use functions that use different parameters, at the cost of less strictness:
> 
>     def func1(arg, *, kw1, **kwargs):
>         ...
> 
>     def func2(arg, *, kw2, **kwargs):
>         ...
> 
> That class is more of a demo for sigtools.signatures.merge[2] rather than something spawned out of a need however.
> 
> [1] http://sigtools.readthedocs.org/en/latest/#sigtools.wrappers.Combination
> [2] http://sigtools.readthedocs.org/en/latest/#sigtools.signatures.merge
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20150506/0023be0f/attachment.html>


More information about the Python-ideas mailing list