But in `for...else` the `else` call isn't always called, so changing `else` for `finally` doesn't make sense. What you're suggesting is replacing `else` with `on_finish` and adding `finally` and `on_break`.
I agree that having `finally` could make the use cases of `else` clearer, but I am not convinced renaming "else" to "on_finish" would help the confusion for the 0 iteration case.
I think that since this suggestion doesn't help with the 0 iteration case (my first idea here didn't either), it feels like added extra compound statements need to be immediately intuitive to be worth having - either because they read like a sentence or parallel existing python e.g. `try-except-else-finally` or `if-elif-else` etc.
On Wed, 15 Jul 2020 at 06:47, Steve Barnes GadgetSteve@live.co.uk wrote:
Can I suggest that for loops the `else` would be a lot clearer if it was spelt `finally` as was done for PEP-0341 for try blocks and that we might possibly need one or more `on_…` clauses such as `on_break` and `on_finish` I think that this would be a lot clearer:
for i in range(N):
if i > 3: break;
on_break: # Called if loop was broken
on_finish: # Called if loop was not broken
finally: # Always called (replaces for…else)
Which I think would be a lot easier for newcomers to learn than try…for…else…except…else e.g.:
for i in range(N): if i > 3: break; elif i % 2 == 0: raise ValueError("Odds Only"); else: # to if print(i) else: # Else to loop print("Loop Completed")
except ValueError as err:
else: # to try
Where the multitude of elses makes my eyes cross.
Steve Barnes _______________________________________________ Python-ideas mailing list -- email@example.com To unsubscribe send an email to firstname.lastname@example.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://email@example.com/message/MKAAWV... Code of Conduct: http://python.org/psf/codeofconduct/