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

Joshua Landau joshua at landau.ws
Tue Jul 16 17:31:48 CEST 2013


On 16 July 2013 16:22, Oscar Benjamin <oscar.j.benjamin at gmail.com> wrote:
> On 16 July 2013 15:48, Masklinn <masklinn at masklinn.net> wrote:
>>>> You could wait for PEP 448, which will let you use [*sublist for
>>>> sublist in list_to_be_flattened].
>>>
>>> Well that does look good. How exactly does it unroll? Does the *
>>> translate as yield from but without the weird comprehension turning
>>> into a generator function behaviour?
>>
>> For a listcomp, surely it translates into a .extend of the accumulator?
>
> So
>     [a for b in c]
> is
>     for b in c:
>         result.append(a)
> and
>     [*a for b in c]
> is
>     for b in c:
>         result.extend(a)

Correct in essence, I don't know how the implementation works.

> Set and dict comps presumably use .update. And the generator expression
>     (*a for b in c)
> becomes
>     for b in c:
>         for x in a:
>             yield x
> or is it actually (this is not equivalent):
>     for b in c:
>        yield from a

I imagine it would use "yield from", although it is not actually
defined in the PEP. I see no reason to prefer the explicit loop. If
this matters enough, I can add it to the PEP, but I'd need a consensus
in order to dictate a specific methodology over others.


More information about the Python-ideas mailing list