This is more of a python-ideas discussion, and Steven's answer is good. 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."
-Ben On Mon, Jul 24, 2017 at 12:14 PM, Steven D'Aprano <st...@pearwood.info> wrote: > Hello Kiuhnm, and welcome. > > On Mon, Jul 24, 2017 at 05:35:03PM +0200, Kiuhnm via Python-Dev wrote: > > Hello, > > > > I think that the expression "for...else" or "while...else" is completely > > counter-intuitive. > > > You may be right -- this has been discussed many, many times before. In > my personal opinion, the best (and only accurate!) phrase would have > been: > > for item in sequence: > # block > then: > # block > > If you look at the byte-code generated by a for...else statement, you > see that the "else" block is unconditionally executed after the for loop > completes, unless something causes a jump outside of the entire > statement: return, break, or raise. So it is more like: > > - run the loop; > - *then* run the following block > > rather than: > > - run the loop; > - otherwise ("else") run the following block. > > Others disagree and would prefer other keywords. But regardless, > backwards compatibility means that we must keep "for...else", so I'm > afraid that discussing alternatives is *almost certainly* a waste of > time. > > > > Wouldn't it be possible to make it clearer? Maybe > > something like > > At this point, no, it is not practical to change the syntax used. Maybe > when Python 3.0 was first introduced, but that ship has long sailed. It > is very, very unlikely that the syntax for this will ever change, but if > it does, it probably won't be until something in the distant future like > Python 5. > > But not Python 4: Guido has already ruled that Python 4 will not include > major backwards-incompatible changes. Going from 3 to 4 will not be as > disruptive as going from 2 to 3. > > So depending on how you look at it: discussing alternative syntax to > for...else is either ten years too late or ten years too early. > > > > -- > Steve > _______________________________________________ > Python-Dev mailing list > Python-Dev@python.org > https://mail.python.org/mailman/listinfo/python-dev > Unsubscribe: https://mail.python.org/mailman/options/python-dev/ > benhoyt%40gmail.com >
_______________________________________________ Python-Dev mailing list Python-Dev@python.org https://mail.python.org/mailman/listinfo/python-dev Unsubscribe: https://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com