[Python-ideas] Another attempt at a sum() alternative: the concatenation protocol

Nick Coghlan ncoghlan at gmail.com
Tue Jul 16 15:01:13 CEST 2013


On 16 July 2013 22:28, Joshua Landau <joshua at landau.ws> wrote:
> On 16 July 2013 11:21, Oscar Benjamin <oscar.j.benjamin at gmail.com> wrote:
>> On 16 July 2013 07:50, Nick Coghlan <ncoghlan at gmail.com> wrote:
>>
>> If people are using sum() to concatenate lists then this should be
>> taken not as evidence that a new solution needs to be found but as
>> evidence that chain is not sufficiently well-known. The obvious
>> solution to that is not to implement a new protocol but to make the
>> existing solution more well known i.e. move chain.from_iterable to
>> builtins and rename it (the obvious choice being concat).
>
> You could wait for PEP 448, which will let you use [*sublist for
> sublist in list_to_be_flattened].

Ah, true, I forgot about that. Too many interesting things going on
for me to keep track of everything :)

In effect, PEP 448 goes further than making chain a builtin: it gives
it syntax! With PEP 448, the generator expression:

    (*itr for itr in iterables)

would be equivalent to either of the current:

    itertools.chain(*iterables)
    itertools.chain.from_iterable(iterables)

That's pretty cool. It also means I can go back to happily ignoring
the sum threads :)

Cheers,
Nick.

P.S. Something about this should probably be added to the rationale
section of PEP 448

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


More information about the Python-ideas mailing list