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 <thomasratzk...@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-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/LNER4MH6IT6HBFKFVTUOJ225PTCZSRRC/
> Code of Conduct: http://python.org/psf/codeofconduct/
>
_______________________________________________
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/WWEJQD7IIPNC4FUSPHLXEH7SVN6EVK6H/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to