Re: Feature Suggestion: "repeat" statement in loops
"Joao S. O. Bueno" <[email protected]> Thu, 26 Jan 2023 17:10:22 -0300
| Newsgroups | gmane.comp.python.devel |
|---|---|
| Message-ID | <CAH0mxTSB6_tqqt3rroMT==uAXp4nSF6Keji3EWJ9KwDuWxLrEg@mail.gmail.com> |
--===============7179144144915401385==
Content-Type: multipart/alternative; boundary="000000000000aa043705f33057a4"
--000000000000aa043705f33057a4
Content-Type: text/plain; charset="UTF-8"
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 <[email protected]>
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 -- [email protected]
> To unsubscribe send an email to [email protected]
> https://mail.python.org/mailman3/lists/python-dev.python.org/
> Message archived at
> https://mail.python.org/archives/list/[email protected]/message/LNER4MH6IT6HBFKFVTUOJ225PTCZSRRC/
> Code of Conduct: http://python.org/psf/codeofconduct/
>
--000000000000aa043705f33057a4
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr">I don't think this will fly - if not for any other rea=
son, it is a very rare pattern=C2=A0<div>to take place alongside such impor=
tant flow-control statements as continue=C2=A0and break</div><div><br>But f=
or your convenience, here is a small wrapper that, along with the=C2=A0<div=
>walrus operator, could be used when you need that functionality:</div><div=
><br></div><div>```</div><div>class Repeatable:<br>=C2=A0 =C2=A0 def __init=
__(self, it):<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 <a href=3D"http://self.it">sel=
f.it</a> =3D it<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 self.repeat_last =3D False<b=
r>=C2=A0 =C2=A0 =C2=A0 =C2=A0 self.last_item =3D None<br>=C2=A0 =C2=A0 def =
repeat(self):<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 self.repeat_last =3D True<br>=
=C2=A0 =C2=A0 def __iter__(self):<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 for item i=
n <a href=3D"http://self.it">self.it</a>:<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =
=C2=A0 =C2=A0 while self.repeat_last:<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0=
=C2=A0 =C2=A0 =C2=A0 self.repeat_last =3D False<br>=C2=A0 =C2=A0 =C2=A0 =
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 yield self.last_item<br>=C2=A0 =C2=A0 =
=C2=A0 =C2=A0 =C2=A0 =C2=A0 self.last_item =3D item<br>=C2=A0 =C2=A0 =C2=A0=
=C2=A0 =C2=A0 =C2=A0 yield item<br><br><br>test =3D 1<br>for x in (rx:=3DR=
epeatable(range(3))):<br>=C2=A0 =C2=A0 print(x)<br>=C2=A0 =C2=A0 if x =3D=
=3D test:<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 test =3D -1<br>=C2=A0 =C2=A0 =C2=
=A0 =C2=A0 rx.repeat()<br></div><div>```</div></div></div><br><div class=3D=
"gmail_quote"><div dir=3D"ltr" class=3D"gmail_attr">On Thu, Jan 26, 2023 at=
4:41 PM Thomas Ratzke <<a href=3D"mailto:[email protected]">tho=
[email protected]</a>> wrote:<br></div><blockquote class=3D"gmail_q=
uote" style=3D"margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,2=
04);padding-left:1ex">Hi all,<br>
<br>
i would like to suggest the following Python feature. It naturally <br>
happens that one want's to repeat the current iteration of a for loop <=
br>
for example after an error happened. For this purpose, I usually set a <br>
flag and put a while loop inside my for loop. A simple "repeat" <=
br>
statement just like "continue" or "break" would make th=
e code much more <br>
readable.<br>
<br>
<br>
This is my solution at the moment with A being checked:<br>
<br>
for _ in range(n):<br>
=C2=A0=C2=A0=C2=A0=C2=A0 flag =3D True<br>
=C2=A0=C2=A0=C2=A0=C2=A0 while flag:<br>
=C2=A0=C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 ...<br>
=C2=A0=C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 if A:<br>
=C2=A0=C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 flag =3D Fal=
se # go to next iteration<br>
<br>
<br>
I would suggest the repeat statement in the following sense<br>
<br>
for _ in range(n):<br>
=C2=A0=C2=A0=C2=A0=C2=A0 ...<br>
=C2=A0=C2=A0=C2=A0=C2=A0 if not A:<br>
=C2=A0=C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 repeat # repeat current iterati=
on<br>
<br>
Notice the "not" in the if clause. I am really looking forwars to=
hear <br>
your opinions.<br>
<br>
Best regards<br>
Thomas<br>
<br>
_______________________________________________<br>
Python-Dev mailing list -- <a href=3D"mailto:[email protected]" target=
=3D"_blank">[email protected]</a><br>
To unsubscribe send an email to <a href=3D"mailto:[email protected]=
rg" target=3D"_blank">[email protected]</a><br>
<a href=3D"https://mail.python.org/mailman3/lists/python-dev.python.org/" r=
el=3D"noreferrer" target=3D"_blank">https://mail.python.org/mailman3/lists/=
python-dev.python.org/</a><br>
Message archived at <a href=3D"https://mail.python.org/archives/list/python=
[email protected]/message/LNER4MH6IT6HBFKFVTUOJ225PTCZSRRC/" rel=3D"noreferre=
r" target=3D"_blank">https://mail.python.org/archives/list/python-dev@pytho=
n.org/message/LNER4MH6IT6HBFKFVTUOJ225PTCZSRRC/</a><br>
Code of Conduct: <a href=3D"http://python.org/psf/codeofconduct/" rel=3D"no=
referrer" target=3D"_blank">http://python.org/psf/codeofconduct/</a><br>
</blockquote></div>
--000000000000aa043705f33057a4--
--===============7179144144915401385==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline