Re: Feature Suggestion: "repeat" statement in loops
Steven D'Aprano <[email protected]> Sun, 29 Jan 2023 12:43:55 +1100
| Newsgroups | gmane.comp.python.devel |
|---|---|
| Message-ID | <[email protected]> |
On Thu, Jan 26, 2023 at 08:34:04PM +0100, Thomas Ratzke wrote: > Hi all, >=20 > i would like to suggest the following Python feature. It naturally=20 > happens that one want's to repeat the current iteration of a for loop=20 > for example after an error happened. Can you give an example of when you would do that? > For this purpose, I usually set a=20 > flag and put a while loop inside my for loop. A simple "repeat"=20 > statement just like "continue" or "break" would make the code much more=20 > readable. Is the idea that it just restarts the current iteration, or that we=20 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=20 attractive nuisance that ends up causing lots of infinite loops. If an=20 error occurs during one iteration, or some other condition, and you=20 re-run that iteration, surely the condition will continue to hold and=20 you will re-run it again, leading to an infinite loop? Unless the error is stochastic (e.g. depends on randomness, or=20 environmental conditions which may change from one loop iteration to the=20 next) repeating the loop won't change the conditions that cause the=20 error. So I see that a `repeat` keyword would make it easy to turn a=20 nice clear exception into an annoying infinite loop. Your sketch of an example shows the problem: > for _ in range(n): > =A0=A0=A0 ... > =A0=A0=A0 if not A: > =A0=A0=A0 =A0=A0=A0 repeat # repeat current iteration Without changing the value of `A`, re-running the current iteration will=20 just hit the `repeat` statement again and again and again, forever. --=20 Steve