On Thu, Jan 26, 2023 at 08:34:04PM +0100, Thomas Ratzke wrote:

> Hi all,
> 
> i would like to suggest the following Python feature. It naturally 
> happens that one want's to repeat the current iteration of a for loop 
> for example after an error happened.

Can you give an example of when you would do that?


> For this purpose, I usually set a 
> flag and put a while loop inside my for loop. A simple "repeat" 
> statement just like "continue" or "break" would make the code much more 
> readable.

Is the idea that it just restarts the current iteration, or that we 
rewind the program state (as in reversible debugging)?

The later would be very difficult.

I think this is an interesting concept, but I fear that it would be an 
attractive nuisance that ends up causing lots of infinite loops. If an 
error occurs during one iteration, or some other condition, and you 
re-run that iteration, surely the condition will continue to hold and 
you will re-run it again, leading to an infinite loop?

Unless the error is stochastic (e.g. depends on randomness, or 
environmental conditions which may change from one loop iteration to the 
next) repeating the loop won't change the conditions that cause the 
error. So I see that a `repeat` keyword would make it easy to turn a 
nice clear exception into an annoying infinite loop.

Your sketch of an example shows the problem:

> for _ in range(n):
>     ...
>     if not A:
>         repeat # repeat current iteration

Without changing the value of `A`, re-running the current iteration will 
just hit the `repeat` statement again and again and again, forever.


-- 
Steve
_______________________________________________
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-le...@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at 
https://mail.python.org/archives/list/python-dev@python.org/message/ZHXYRM4LN6UUFGGHIGGY5PTNWQSWJOY4/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to