if blah:
for .. in ..:
..
else:
..
is visually confused with: for .. in ..:
..
else:
..
What I'm saying is that for-else and while-else add syntax to the language that only solves a single specific instance of a class of similar issues, making it confusing.Let's consider the issues that are in the same class:
- execute code block when loop condition isn't met the first time (i.e. loop never executes).
- execute code block when loop exits prematurely (e.g. break).
- execute code block when loop doesn't exit prematurely (e.g. no break) // execute code block when loop condition evals to false (first eval or any subsequent eval)
Only one of these issues is solved by the for-else/while-else syntax currently in Python, and using a generic keyword (else) just heightens the confusion (especially since this syntax differs from many other languages).
All of these cases may require the use of sentinels / additional checks to implement. Why is one specific instance any more relevant than the others to the point that it gets special treatment (i.e. special syntax in the core language)?
For example, I think that it's more common to want to do something like:
Than: And what about conditional code that you want to execute when a loop does break early?