Re: Feature Suggestion: "repeat" statement in loops

Matthias Görgens <[email protected]> Sat, 28 Jan 2023 10:44:12 +0100
Newsgroups gmane.comp.python.devel
Message-ID <CA+X7ob9gLcuAt+osRnuhPe7te+hDP7O+wm_3yjaDyD0VU+jZzA@mail.gmail.com>
--===============7931757169376536401==
Content-Type: multipart/alternative; boundary="000000000000c4ed0105f34fd3cd"

--000000000000c4ed0105f34fd3cd
Content-Type: text/plain; charset="UTF-8"

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, <[email protected]> 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 <[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/
>>
> _______________________________________________
> 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/WWEJQD7IIPNC4FUSPHLXEH7SVN6EVK6H/
> Code of Conduct: http://python.org/psf/codeofconduct/
>

--000000000000c4ed0105f34fd3cd
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"auto"><div>I don&#39;t think &#39;repeat&#39; should make it in=
to the language.</div><div dir=3D"auto"><br></div><div dir=3D"auto">But: it=
&#39;s an excellent test case to check how flexible the language already is=
. Joao did a great job demonstrating what&#39;s possible!<br><br><div class=
=3D"gmail_quote" dir=3D"auto"><div dir=3D"ltr" class=3D"gmail_attr">On Thu,=
 26 Jan 2023, 21:15 Joao S. O. Bueno, &lt;<a href=3D"mailto:[email protected]=
om">[email protected]</a>&gt; wrote:<br></div><blockquote class=3D"gmail_qu=
ote" style=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex=
"><div dir=3D"ltr">I don&#39;t think this will fly - if not for any other r=
eason, it is a very rare pattern=C2=A0<div>to take place alongside such imp=
ortant flow-control statements as continue=C2=A0and break</div><div><br>But=
 for your convenience, here is a small wrapper that, along with the=C2=A0<d=
iv>walrus operator, could be used when you need that functionality:</div><d=
iv><br></div><div>```</div><div>class Repeatable:<br>=C2=A0 =C2=A0 def __in=
it__(self, it):<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 <a href=3D"http://self.it" t=
arget=3D"_blank" rel=3D"noreferrer">self.it</a> =3D it<br>=C2=A0 =C2=A0 =C2=
=A0 =C2=A0 self.repeat_last =3D False<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 self.l=
ast_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):<b=
r>=C2=A0 =C2=A0 =C2=A0 =C2=A0 for item in <a href=3D"http://self.it" target=
=3D"_blank" rel=3D"noreferrer">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 &lt;<a href=3D"mailto:[email protected]" tar=
get=3D"_blank" rel=3D"noreferrer">[email protected]</a>&gt; wrote:<=
br></div><blockquote class=3D"gmail_quote" style=3D"margin:0px 0px 0px 0.8e=
x;border-left:1px solid rgb(204,204,204);padding-left:1ex">Hi all,<br>
<br>
i would like to suggest the following Python feature. It naturally <br>
happens that one want&#39;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 &quot;repeat&quot; <=
br>
statement just like &quot;continue&quot; or &quot;break&quot; 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 &quot;not&quot; 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" rel=3D"noreferrer">[email protected]</a><br>
To unsubscribe send an email to <a href=3D"mailto:[email protected]=
rg" target=3D"_blank" rel=3D"noreferrer">[email protected]</a><br=
>
<a href=3D"https://mail.python.org/mailman3/lists/python-dev.python.org/" r=
el=3D"noreferrer noreferrer" target=3D"_blank">https://mail.python.org/mail=
man3/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 noreferrer" target=3D"_blank">https://mail.python.org/archives/list/pytho=
[email protected]/message/LNER4MH6IT6HBFKFVTUOJ225PTCZSRRC/</a><br>
Code of Conduct: <a href=3D"http://python.org/psf/codeofconduct/" rel=3D"no=
referrer noreferrer" target=3D"_blank">http://python.org/psf/codeofconduct/=
</a><br>
</blockquote></div>
_______________________________________________<br>
Python-Dev mailing list -- <a href=3D"mailto:[email protected]" target=
=3D"_blank" rel=3D"noreferrer">[email protected]</a><br>
To unsubscribe send an email to <a href=3D"mailto:[email protected]=
rg" target=3D"_blank" rel=3D"noreferrer">[email protected]</a><br=
>
<a href=3D"https://mail.python.org/mailman3/lists/python-dev.python.org/" r=
el=3D"noreferrer noreferrer" target=3D"_blank">https://mail.python.org/mail=
man3/lists/python-dev.python.org/</a><br>
Message archived at <a href=3D"https://mail.python.org/archives/list/python=
[email protected]/message/WWEJQD7IIPNC4FUSPHLXEH7SVN6EVK6H/" rel=3D"noreferre=
r noreferrer" target=3D"_blank">https://mail.python.org/archives/list/pytho=
[email protected]/message/WWEJQD7IIPNC4FUSPHLXEH7SVN6EVK6H/</a><br>
Code of Conduct: <a href=3D"http://python.org/psf/codeofconduct/" rel=3D"no=
referrer noreferrer" target=3D"_blank">http://python.org/psf/codeofconduct/=
</a><br>
</blockquote></div></div></div>

--000000000000c4ed0105f34fd3cd--

--===============7931757169376536401==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline