[Python-Dev] Feature Suggestion: "repeat" statement in loops

11 views
Skip to first unread message

Thomas Ratzke

unread,
Jan 26, 2023, 2:46:12 PM1/26/23
to pytho...@python.org
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 -- pytho...@python.org
To unsubscribe send an email to python-d...@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at https://mail.python.org/archives/list/pytho...@python.org/message/LNER4MH6IT6HBFKFVTUOJ225PTCZSRRC/
Code of Conduct: http://python.org/psf/codeofconduct/

Joao S. O. Bueno

unread,
Jan 26, 2023, 3:15:34 PM1/26/23
to Thomas Ratzke, pytho...@python.org
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()
```

Chris Angelico

unread,
Jan 26, 2023, 5:13:07 PM1/26/23
to pytho...@python.org
On Fri, 27 Jan 2023 at 06:42, Thomas Ratzke <thomasr...@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
_______________________________________________
Python-Dev mailing list -- pytho...@python.org
To unsubscribe send an email to python-d...@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at https://mail.python.org/archives/list/pytho...@python.org/message/BG3DUXYTZLMZAO3UK545C5YXNY3AA3VA/

Jen Kris via Python-Dev

unread,
Jan 27, 2023, 7:44:33 PM1/27/23
to Thomas Ratzke, pytho...@python.org
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 thomasr...@outlook.de:

Matthias Görgens

unread,
Jan 28, 2023, 7:32:17 PM1/28/23
to gwi...@gmail.com, Thomas Ratzke, pytho...@python.org
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!

Steven D'Aprano

unread,
Jan 28, 2023, 8:46:38 PM1/28/23
to pytho...@python.org
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 -- pytho...@python.org
To unsubscribe send an email to python-d...@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at https://mail.python.org/archives/list/pytho...@python.org/message/ZHXYRM4LN6UUFGGHIGGY5PTNWQSWJOY4/
Reply all
Reply to author
Forward
0 new messages