3 ms·
I like it! Makes perfect sense, it's short, and since then "belongs to" the for, it makes sense for a break or exception to break out of both. On the other han
by logicallee 8y ago
I like it! Makes perfect sense, it's short, and since then "belongs to" the for, it makes sense for a break or exception to break out of both. On the other hand, under your suggestion someone still has to learn the meaning of then, it's not fully explicit. Since "Explicit is better than Implicit", if we are not optimizing for brevity I have these two suggestions that are more explicit alternatives to your then suggestion.
--------
Suggestion 1 (horrific suggestion):
Call this keyword finally. This implies that no matter what the finally part will execute. This is like defining never as a synonym for true, so that if(never) always executes. Why would anyone ever do that!! Hardly any choice in a design is as bad as explicitly saying something different from what will happen. So this is a terrible suggestion.
--------
Suggestion 2 (good suggestion):
Call this keyword ifthru. (Thru being an abbreviation for through). As it says, this one will run if the loop completed. Another way to read it is if execution continued "through" the end.
So your example might read:
for i in l:
print(i)
ifthru:
print("iteration finished without breaking or exceptions")
This is extremely explicit about the condition. I picked it to be short while remaining explicit. The elif keyword is an existing precedent for mispelling for the sake of brevity.
It also alerts the reader that logically there is a condition here. After all, semantically every block could start with the "then" word in English. Semantically, when you break you could also be considered to be "through" however in this case the ifthru keyword wouldn't serve any purpose at all, since it could just be on the next line. The "if" alerts the reader that there's a condition here..
I hope you'll agree that my second suggestion is pretty good :)