<div dir="ltr">This is more of a python-ideas discussion, and Steven's answer is good.<div><br></div><div>I'll just add one thing. Maybe it's obvious to others, but I've liked for...else since I found a kind of mnemonic to help me remember when the "else" part happens: I think of it not as "for ... else" but as "break ... else" -- saying it this way makes it clear to me that the break goes with the else. "If this condition inside the loop is true, break. ... *else* if we didn't break, do this other thing after the loop."</div><div><div><br></div><div>-Ben</div></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Mon, Jul 24, 2017 at 12:14 PM, Steven D'Aprano <span dir="ltr"><<a href="mailto:steve@pearwood.info" target="_blank">steve@pearwood.info</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hello Kiuhnm, and welcome.<br>
<span class=""><br>
On Mon, Jul 24, 2017 at 05:35:03PM +0200, Kiuhnm via Python-Dev wrote:<br>
> Hello,<br>
><br>
> I think that the expression "for...else" or "while...else" is completely<br>
> counter-intuitive.<br>
<br>
<br>
</span>You may be right -- this has been discussed many, many times before. In<br>
my personal opinion, the best (and only accurate!) phrase would have<br>
been:<br>
<br>
for item in sequence:<br>
    # block<br>
then:<br>
    # block<br>
<br>
If you look at the byte-code generated by a for...else statement, you<br>
see that the "else" block is unconditionally executed after the for loop<br>
completes, unless something causes a jump outside of the entire<br>
statement: return, break, or raise. So it is more like:<br>
<br>
- run the loop;<br>
- *then* run the following block<br>
<br>
rather than:<br>
<br>
- run the loop;<br>
- otherwise ("else") run the following block.<br>
<br>
Others disagree and would prefer other keywords. But regardless,<br>
backwards compatibility means that we must keep "for...else", so I'm<br>
afraid that discussing alternatives is *almost certainly* a waste of<br>
time.<br>
<span class=""><br>
<br>
> Wouldn't it be possible to make it clearer? Maybe<br>
> something like<br>
<br>
</span>At this point, no, it is not practical to change the syntax used. Maybe<br>
when Python 3.0 was first introduced, but that ship has long sailed. It<br>
is very, very unlikely that the syntax for this will ever change, but if<br>
it does, it probably won't be until something in the distant future like<br>
Python 5.<br>
<br>
But not Python 4: Guido has already ruled that Python 4 will not include<br>
major backwards-incompatible changes. Going from 3 to 4 will not be as<br>
disruptive as going from 2 to 3.<br>
<br>
So depending on how you look at it: discussing alternative syntax to<br>
for...else is either ten years too late or ten years too early.<br>
<span class="HOEnZb"><font color="#888888"><br>
<br>
<br>
--<br>
Steve<br>
</font></span><div class="HOEnZb"><div class="h5">______________________________<wbr>_________________<br>
Python-Dev mailing list<br>
<a href="mailto:Python-Dev@python.org">Python-Dev@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/python-dev" rel="noreferrer" target="_blank">https://mail.python.org/<wbr>mailman/listinfo/python-dev</a><br>
Unsubscribe: <a href="https://mail.python.org/mailman/options/python-dev/benhoyt%40gmail.com" rel="noreferrer" target="_blank">https://mail.python.org/<wbr>mailman/options/python-dev/<wbr>benhoyt%40gmail.com</a><br>
</div></div></blockquote></div><br></div>