[Python-ideas] Temporary variables in comprehensions

Kyle Lahnakoski klahnakoski at mozilla.com
Fri Feb 23 13:39:06 EST 2018



On 2018-02-23 12:44, Neil Girdhar wrote:
>
> On Fri, Feb 23, 2018 at 12:35 PM Kyle Lahnakoski
> <klahnakoski at mozilla.com <mailto:klahnakoski at mozilla.com>> wrote:
>
>
>     > [
>     >     (w, w**2)
>     >     for x in (1, 2, 3, 4)
>     >     let y = x+1
>     >     for a in range(y)
>     >     let z = a+1
>     >     if z > 2
>     >     for b in range(z)
>     >     let w = z+1
>     > ]
>
>     which is a short form for:
>
>     > def stuff():
>     >     for x in (1, 2, 3, 4):
>     >         y = x+1
>     >         for a in range(y):
>     >             z = a+1
>     >             if z > 2:
>     >                 for b in range(z):
>     >                     w = z+1
>     >                     yield (w, w**2)
>     >
>     > list(stuff())
>
>
> Is it that much shorter that it's worth giving up the benefit of
> indentation? 
>
>

Saving the indentation? Oh yes, for sure!  This code reads like a story,
the indentation is superfluous to that story.  Should we add it to
Python? I don't know; I quick scan through my own code, and I do not see
much opportunity for list comprehensions of this complexity.  Either my
data structures are not that complicated, or I have try/except blocks
inside a loop, or I am using a real query language (like SQL).  pythonql
seems to solve all these problems well enough
(https://github.com/pythonql/pythonql).

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20180223/144a2527/attachment-0001.html>


More information about the Python-ideas mailing list