On Fri, Feb 23, 2018 at 12:35 PM Kyle Lahnakoski <klahnakoski@mozilla.com> wrote:
I believe list comprehensions are difficult to read because they are not
formatted properly. For me, list comprehension clauses are an
expression, followed by clauses executed in the order. Any list
comprehension with more than one clause should be one-line-per clause.

Examples inline:

On 2018-02-15 19:57, Steven D'Aprano wrote:
> Where should the assignment go? [(y, y**2) let y = x+1 for x in (1, 2,
> 3, 4)] [(y, y**2) for x in (1, 2, 3, 4) let y = x+1]

Since y is a function of x, it must follow the for clause:

> [
>     (y, y ** 2)
>     for x in (1, 2, 3, 4)
>     let y = x + 1
> ]

> How do they interact when you have multiple loops and if-clauses? [(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]

They are applied in order:

> [
>     (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? 


_______________________________________________
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/

--

---
You received this message because you are subscribed to a topic in the Google Groups "python-ideas" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/python-ideas/KwZtO4rpAGE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to python-ideas+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.