[Python-ideas] Function composition (was no subject)
Steven D'Aprano
steve at pearwood.info
Sun May 10 04:56:30 CEST 2015
On Sat, May 09, 2015 at 10:15:21PM +0300, Koos Zevenhoven wrote:
>
> On 2015-05-09 21:16, Steven D'Aprano wrote:
[...]
> >It's a pity we can't match the shell syntax and write:
> >
> >spam(args)|eggs|cheese
> >
> >but that would have a completely different meaning.
>
> But it does not need to have a different meaning.
It *should* have a different meaning. I want it to have a different
meaning. Python is not the shell and spam(args) could be a factory
function which itself returns a callable, e.g. partial, or a decorator.
We cannot match the shell syntax because Python can do so much more than
the shell.
> You could in addition have:
>
> spam @ eggs @ cheese @ arg # equivalent to spam(eggs(cheese(arg)))
>
> arg | spam | eggs | cheese # equivalent to cheese(eggs(spam(arg)))
>
> Here, arg would thus be recognized as not a function.
No. I think it is absolutely vital to distinguish by syntax the
difference between composition and function application, and not try to
"do what I mean". DWIM software has a bad history of doing the wrong
thing.
Every other kind of callable uses obj(arg) to call it: types, functions,
methods, partial objects, etc. We shouldn't make function composition
try to be different. If I write sqrt at 100 I should get a runtime error,
not 10.
I don't mind if the error is delayed until I actually try to call the
composed object, but at some point I should get a TypeError that 100 is
not callable.
--
Steve
More information about the Python-ideas
mailing list