Feature Suggestion: "repeat" statement in loops
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. 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. This is my solution at the moment with A being checked: for _ in range(n): flag = True while flag: ... if A: flag = False # go to next iteration I would suggest the repeat statement in the following sense for _ in range(n): ... if not A: repeat # repeat current iteration Notice the "not" in the if clause. I am really looking forwars to hear your opinions. Best regards Thomas
I don't think this will fly - if not for any other reason, it is a very rare pattern to take place alongside such important flow-control statements as continue and break But for your convenience, here is a small wrapper that, along with the walrus operator, could be used when you need that functionality: ``` class Repeatable: def __init__(self, it): self.it = it self.repeat_last = False self.last_item = None def repeat(self): self.repeat_last = True def __iter__(self): for item in self.it: while self.repeat_last: self.repeat_last = False yield self.last_item self.last_item = item yield item test = 1 for x in (rx:=Repeatable(range(3))): print(x) if x == test: test = -1 rx.repeat() ``` On Thu, Jan 26, 2023 at 4:41 PM Thomas Ratzke <thomasratzketr@outlook.de> 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. 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.
This is my solution at the moment with A being checked:
for _ in range(n): flag = True while flag: ... if A: flag = False # go to next iteration
I would suggest the repeat statement in the following sense
for _ in range(n): ... if not A: repeat # repeat current iteration
Notice the "not" in the if clause. I am really looking forwars to hear your opinions.
Best regards Thomas
_______________________________________________ Python-Dev mailing list -- python-dev@python.org To unsubscribe send an email to python-dev-leave@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/LNER4MH6... Code of Conduct: http://python.org/psf/codeofconduct/
I don't think 'repeat' should make it into the language. But: it's an excellent test case to check how flexible the language already is. Joao did a great job demonstrating what's possible! On Thu, 26 Jan 2023, 21:15 Joao S. O. Bueno, <gwidion@gmail.com> wrote:
I don't think this will fly - if not for any other reason, it is a very rare pattern to take place alongside such important flow-control statements as continue and break
But for your convenience, here is a small wrapper that, along with the walrus operator, could be used when you need that functionality:
``` class Repeatable: def __init__(self, it): self.it = it self.repeat_last = False self.last_item = None def repeat(self): self.repeat_last = True def __iter__(self): for item in self.it: while self.repeat_last: self.repeat_last = False yield self.last_item self.last_item = item yield item
test = 1 for x in (rx:=Repeatable(range(3))): print(x) if x == test: test = -1 rx.repeat() ```
On Thu, Jan 26, 2023 at 4:41 PM Thomas Ratzke <thomasratzketr@outlook.de> 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. 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.
This is my solution at the moment with A being checked:
for _ in range(n): flag = True while flag: ... if A: flag = False # go to next iteration
I would suggest the repeat statement in the following sense
for _ in range(n): ... if not A: repeat # repeat current iteration
Notice the "not" in the if clause. I am really looking forwars to hear your opinions.
Best regards Thomas
_______________________________________________ Python-Dev mailing list -- python-dev@python.org To unsubscribe send an email to python-dev-leave@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/LNER4MH6... Code of Conduct: http://python.org/psf/codeofconduct/
_______________________________________________ Python-Dev mailing list -- python-dev@python.org To unsubscribe send an email to python-dev-leave@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/WWEJQD7I... Code of Conduct: http://python.org/psf/codeofconduct/
On Fri, 27 Jan 2023 at 06:42, Thomas Ratzke <thomasratzketr@outlook.de> 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. 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.
This is my solution at the moment with A being checked:
for _ in range(n): flag = True while flag: ... if A: flag = False # go to next iteration
Why not use break? ChrisA
Why would "if not A" also be true when you repeat the current iteration? What keeps this from becoming an endless loop? Jan 26, 2023, 11:45 by thomasratzketr@outlook.de:
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. 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.
This is my solution at the moment with A being checked:
for _ in range(n): flag = True while flag: ... if A: flag = False # go to next iteration
I would suggest the repeat statement in the following sense
for _ in range(n): ... if not A: repeat # repeat current iteration
Notice the "not" in the if clause. I am really looking forwars to hear your opinions.
Best regards Thomas
_______________________________________________ Python-Dev mailing list -- python-dev@python.org To unsubscribe send an email to python-dev-leave@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/LNER4MH6... Code of Conduct: http://python.org/psf/codeofconduct/
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
participants (6)
-
Chris Angelico
-
Jen Kris
-
Joao S. O. Bueno
-
Matthias Görgens
-
Steven D'Aprano
-
Thomas Ratzke