[Python-ideas] Fwd: Fwd: Fwd: unpacking generalisations for list comprehension

David Mertz mertz at gnosis.cx
Mon Oct 17 14:48:23 EDT 2016


On Mon, Oct 17, 2016 at 10:32 AM, Steven D'Aprano <steve at pearwood.info>
wrote:

> But if we *do* accept this syntax, then I believe that we should drop
> the pretense that it is a natural extension of sequence unpacking in the
> context of a for-loop-with-append (i.e. list comprehensions) and accept
> that it will be seen by people as a magical "flatten" operator. [...]
>
So, yet again for emphasis: I see what you mean about unrolling the list
> comprehension into a list display. But I believe that's not a helpful
> way to think about list comprehensions.


Moreover, this "magical flatten" operator will crash in bad ways that a
regular flatten() will not.  I.e. this is fine (if strange):

>>> three_inf = (count(), count(), count())
>>> comp = (x for x in flatten(three_inf))
>>> next(comp)
0
>>> next(comp)
1

It's hard to see how that won't blow up under the new syntax (i.e.
generally for all infinite sequences).

Try running this, for example:

>>> a, *b = count()

Syntactically valid... but doesn't terminate.

-- 
Keeping medicines from the bloodstreams of the sick; food
from the bellies of the hungry; books from the hands of the
uneducated; technology from the underdeveloped; and putting
advocates of freedom in prisons.  Intellectual property is
to the 21st century what the slave trade was to the 16th.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20161017/1ada834b/attachment.html>


More information about the Python-ideas mailing list