<div dir="ltr">One does not seem to be able to do this in a generator expression:<div><br></div><div>foo = (x for x in [1, 2] if True else [1, 2, 3])</div><div><br></div><div>gives a syntax error, however, adding parenthesis 'solves' this:</div><div><br></div><div>foo = (x for x in [1, 2] if True else [1, 2, 3])<br></div><div><br></div><div>In the for-loop version, either works. Though I guess this would be even more frowned upon, one can even do:</div><div><br></div><div>for x in [], print("Here be dragons"):</div><div> pass</div><div><br></div><div>Whether it can be implemented in an LL(1) parser, my gut says it could, but this does complicate matters if one were to continue supporting it and create a whirl of confusion.</div><div><br></div><div>If anything, this shows that there could have been scope for more consistency between the for-statement and generator expression by enforcing parenthesis in the for-loop as well but I think the grammar as it is will be here to stay, unless there is going to be a Python 4 like there is Python 3...</div><div><br></div><div>I think to be honest this is a pretty big nail in the coffin.</div><div><br></div><div>H-J</div><div><br></div><div><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On 23 February 2017 at 14:51, Jelle Zijlstra <span dir="ltr"><<a href="mailto:jelle.zijlstra@gmail.com" target="_blank">jelle.zijlstra@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class="">2017-02-23 5:37 GMT-08:00 Henk-Jaap Wagenaar <<a href="mailto:wagenaarhenkjaap@gmail.com">wagenaarhenkjaap@gmail.com</a>>:<br>
><br>
> Hi all,<br>
><br>
> Often I have typed something like<br>
><br>
> for x in range(100) if is_prime(x):<br>
> # do things with x<br>
><br>
> to find that this does not work, instead resorting to:<br>
><br>
> for x in range(100):<br>
> if is_prime(x):<br>
> # do things with x<br>
><br>
> or<br>
><br>
> for x in range(100):<br>
> if not is_prime(x):<br>
> continue<br>
> # do things with x<br>
><br>
> Other solutions to another case of this 'problem' are discussed has been discussed on StackOverflow (<a href="http://stackoverflow.com/questions/6981717/pythonic-way-to-combine-for-loop-and-if-statement" rel="noreferrer" target="_blank">http://stackoverflow.com/<wbr>questions/6981717/pythonic-<wbr>way-to-combine-for-loop-and-<wbr>if-statement</a>) where it is suggested one uses a generator expression before the loop. None of these solutions seem very Pythonic to me.<br>
><br>
> I appreciate there is a cost associated with changing the language syntax, and I do not understand all the finer details of the inner workings involved with the Python language development, however in my limited understanding in it would be:<br>
> - fully backwards compatible,<br>
> - require one to change "expression_list" to "or_test [comp_iter]" in the syntax of the for statement (if I got it right).<br>
> - it would mean there is a Pythonic solution to a current 'problem' that does not have one.<br>
><br>
> A few problems I foresee:<br>
> - One wants for loops to bleed their target_list (that is the point normally), so this is different from generators,<br>
> - This allows for nesting of generators, like in a generator expression which might be hard to implement?<br>
><br>
</span>I think it might also be difficult to parse. Python currently supports<br>
this syntax:<br>
<br>
In [8]: for x in [1, 2] if True else [1, 2, 3]:<br>
...: print(x)<br>
...:<br>
1<br>
2<br>
<br>
I'm not sure the parser could distinguish this structure from the one<br>
you propose (which would have a very different meaning) without making<br>
it significantly more complicated. Changes that make the parser more<br>
complicated than LL(1) have been consistently rejected in the past;<br>
see <a href="https://www.python.org/dev/peps/pep-3099/" rel="noreferrer" target="_blank">https://www.python.org/dev/<wbr>peps/pep-3099/</a>.<br>
<span class="im HOEnZb"><br>
><br>
> Note that this has been suggested before at least once (<a href="https://mail.python.org/pipermail/python-dev/2007-November/075257.html" rel="noreferrer" target="_blank">https://mail.python.org/<wbr>pipermail/python-dev/2007-<wbr>November/075257.html</a>), and that thread itself suggests it has been suggested before and shutdown by Guido (though no source is given for this).<br>
><br>
> All the best,<br>
><br>
> Henk-Jaap Wagenaar<br>
><br>
</span><div class="HOEnZb"><div class="h5">> ______________________________<wbr>_________________<br>
> Python-ideas mailing list<br>
> <a href="mailto:Python-ideas@python.org">Python-ideas@python.org</a><br>
> <a href="https://mail.python.org/mailman/listinfo/python-ideas" rel="noreferrer" target="_blank">https://mail.python.org/<wbr>mailman/listinfo/python-ideas</a><br>
> Code of Conduct: <a href="http://python.org/psf/codeofconduct/" rel="noreferrer" target="_blank">http://python.org/psf/<wbr>codeofconduct/</a><br>
</div></div></blockquote></div><br></div>