[Python-ideas] Function composition (was no subject)
Chris Angelico
rosuav at gmail.com
Sun May 10 12:17:09 CEST 2015
On Sun, May 10, 2015 at 7:57 PM, Steven D'Aprano <steve at pearwood.info> wrote:
>> So I would strongly suggest having some sort of operator in between.
>> Okay. Can I just say something crazy? (Hans: I love crazy!) How about
>> using a comma?
>>
>> >>> (fn1, fn2, fn3, ...)('string to be piped')
>>
>> Currently, this produces a runtime TypeError: 'tuple' object is not
>> callable, but I could easily define my own callable subclass of tuple.
>
> There's lots of code that assumes that a tuple of functions is a
> sequence:
>
> for f in (len, str, ord, chr, repr):
> test(f)
>
> so we would need to keep that. But we don't want a composed function to
> be a sequence, any more than we want a partial or a regular function to
> be sequences. If I pass you a Composed object, and you try slicing it,
> that should be an error.
Well, I told you it was crazy :) But the significance here is that
there would be no Composed object, just a tuple. You could slice it,
iterate over it, etc; and if you call it, it calls each of its
arguments. I'm not sure that it's a fundamental problem for a composed
function to be sliceable, any more than it's a problem for any other
available operation that you aren't using. Tuples already have several
related uses (they can be used as "record" types, or as frozen lists
for hashability, etc), and this would simply mean that a tuple of
callables is callable.
ChrisA
More information about the Python-ideas
mailing list