[Python-ideas] Is this PEP-able? for X in ListY while conditionZ:
Daniel Robinson
gottagetmac at gmail.com
Tue Jul 2 05:27:51 CEST 2013
In addition to defying the ordinary order of the syntax, that would make
this statement totally ambiguous:
[f(x), f(y) for x in list1 for y in list2, while x < y]
And I definitely don't think commas (other than the f(x), f(y) usage shown
above) or semicolons are good ideas.
On Mon, Jul 1, 2013 at 10:53 PM, SpaghettiToastBook . <
spaghettitoastbook at gmail.com> wrote:
> What if the while clause went after the rest of the comprehension,
> preceded by a comma?
>
> [f(x) for x in list, while x < 10]
>
> — SpaghettiToastBook
>
>
> On Mon, Jul 1, 2013 at 10:28 PM, Daniel Robinson <gottagetmac at gmail.com>
> wrote:
> > While this looks attractive to me, and it's definitely better to change
> > statement and comprehension syntax at the same time, this makes the
> > comprehension ambiguous to human parsing.
> >
> > [f(x) for x in list if x > 10] basically can be read as
> >
> > for x in list:
> > if x > 10:
> > f(x)
> >
> > This kind of interpretation becomes critical if you nest more than two
> > levels. But [f(x) for x in list while x < 10] could read either as
> >
> > for x in list while x < 10:
> > f(x)
> >
> > which is how you want it to be read, or (more in line with earlier list
> comp
> > habits):
> >
> > for x in list:
> > while x < 10:
> > f(x)
> >
> > which would be totally wrong.
> >
> > I don't think this is a very serious problem (certainly not for the
> > interpreter), but it's a stumbling block.
> >
> > On Mon, Jul 1, 2013 at 10:03 PM, Jan Kaliszewski <zuo at chopin.edu.pl>
> wrote:
> >>
> >> 2013-07-02 00:44, Oscar Benjamin wrote:
> >> [...]
> >>
> >>> Having a while clause on for loops is not just good because it saves a
> >>> couple of lines but because it clearly separates the flow control from
> >>> the body of the loop (another reason I dislike 'break if'). In other
> >>> words I find the flow of the loop
> >>>
> >>> for p in primes() while p < 100:
> >>> print(p)
> >>>
> >>> easier to understand (immediately) than
> >>>
> >>> for p in primes():
> >>> if p >= 100:
> >>> break
> >>> print(p)
> >>
> >>
> >> +1
> >>
> >> Cheers.
> >> *j
> >>
> >>
> >> _______________________________________________
> >> Python-ideas mailing list
> >> Python-ideas at python.org
> >> http://mail.python.org/mailman/listinfo/python-ideas
> >
> >
> >
> > _______________________________________________
> > Python-ideas mailing list
> > Python-ideas at python.org
> > http://mail.python.org/mailman/listinfo/python-ideas
> >
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20130701/3bfc7627/attachment.html>
More information about the Python-ideas
mailing list