[Python-Dev] for...else
Steven D'Aprano
steve at pearwood.info
Mon Jul 24 12:14:48 EDT 2017
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
More information about the Python-Dev
mailing list