<div dir="ltr"><div class="gmail_default" style="font-family:tahoma,sans-serif;color:#000000">It is so true!</div><div class="gmail_default" style="font-family:tahoma,sans-serif;color:#000000">thanks for pointing that out. It makes sense to do it that way</div><div class="gmail_default" style="font-family:tahoma,sans-serif;color:#000000">I probably never used while in different places than ``while 1: pass`` until now.<br></div><div class="gmail_default" style="font-family:tahoma,sans-serif;color:#000000"><br></div><div class="gmail_default" style="font-family:tahoma,sans-serif;color:#000000">I admit, I am looking for something alternative to a for structure like  ``for _ in range(10)`` -- I don't like the ``_`` ;-)</div><div class="gmail_default" style="font-family:tahoma,sans-serif;color:#000000">How can I use the iterator protocoll to make a nice repeat syntax?</div><div class="gmail_default" style="font-family:tahoma,sans-serif;color:#000000"><br></div><div class="gmail_extra"><br><div class="gmail_quote">On 9 September 2015 at 19:15, Joseph Jevnik <span dir="ltr"><<a href="mailto:joejev@gmail.com" target="_blank">joejev@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div><div><div><div><div><div>This appears as intended. The body of the while condition is executed each time the condition is checked. In the first case, you are creating a single instance of repeat, and then calling bool on the expression with each iteration of the loop. With the second case, you are constructing a _new_ repeat instance each time. Think about the difference between:<br><br></div>while should_stop():<br>    ...<br><br></div>and:<br></div>a = should_stop()<br></div>while a:<br>    ...<br><br></div>One would expect should_stop to be called each time in the first case; but, in the second case it is only called once.<br><br></div>With all that said, I think you want to use the __iter__ and __next__ protocols to implement this in a more supported way.<br></div><div class="gmail_extra"><br><div class="gmail_quote"><div><div>On Wed, Sep 9, 2015 at 1:10 PM, Stephan Sahm <span dir="ltr"><<a href="mailto:Stephan.Sahm@gmx.de" target="_blank">Stephan.Sahm@gmx.de</a>></span> wrote:<br></div></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div><div><div dir="ltr"><div class="gmail_default" style="font-family:tahoma,sans-serif;color:rgb(0,0,0)">Dear all</div><div class="gmail_default" style="font-family:tahoma,sans-serif;color:rgb(0,0,0)"><br></div><div class="gmail_default" style="font-family:tahoma,sans-serif;color:rgb(0,0,0)">I found a BUG in the standard while statement, which appears both in python 2.7 and python 3.4 on my system.</div><div class="gmail_default" style="font-family:tahoma,sans-serif;color:rgb(0,0,0)"><br></div><div class="gmail_default" style="font-family:tahoma,sans-serif;color:rgb(0,0,0)">It usually won't appear because I only stumbled upon it after trying to implement a nice repeat structure. Look:</div><div class="gmail_default" style="font-family:tahoma,sans-serif;color:rgb(0,0,0)">​```​</div>class repeat(object):<br>    def __init__(self, n):<br>        self.n = n<br><br>    def __bool__(self):<br>        self.n -= 1<br>        return self.n >= 0<br><br>    __nonzero__=__bool__<div class="gmail_default" style="font-family:tahoma,sans-serif;color:rgb(0,0,0)"><br></div><div class="gmail_default" style="font-family:tahoma,sans-serif;color:rgb(0,0,0)">a = repeat(2)</div><div class="gmail_default" style="font-family:tahoma,sans-serif;color:rgb(0,0,0)">```</div><div class="gmail_default" style="font-family:tahoma,sans-serif;color:rgb(0,0,0)">the meaning of the above is that bool(a) returns True 2-times, and after that always False.</div><div class="gmail_default" style="font-family:tahoma,sans-serif;color:rgb(0,0,0)"><br></div><div class="gmail_default" style="font-family:tahoma,sans-serif;color:rgb(0,0,0)">Now executing</div><div class="gmail_default" style="font-family:tahoma,sans-serif;color:rgb(0,0,0)">```</div><div class="gmail_default" style="font-family:tahoma,sans-serif;color:rgb(0,0,0)">while a:</div><div class="gmail_default" style="font-family:tahoma,sans-serif;color:rgb(0,0,0)">    print('foo')</div><div class="gmail_default" style="font-family:tahoma,sans-serif;color:rgb(0,0,0)">```</div><div class="gmail_default" style="font-family:tahoma,sans-serif;color:rgb(0,0,0)">will in fact print 'foo' two times. HOWEVER ;-) ....</div><div class="gmail_default" style="font-family:tahoma,sans-serif;color:rgb(0,0,0)">```</div><div class="gmail_default" style="font-family:tahoma,sans-serif;color:rgb(0,0,0)">while repeat(2):</div><div class="gmail_default" style="font-family:tahoma,sans-serif;color:rgb(0,0,0)">    print('foo')</div><div class="gmail_default" style="font-family:tahoma,sans-serif;color:rgb(0,0,0)">```</div><div class="gmail_default" style="font-family:tahoma,sans-serif;color:rgb(0,0,0)">will go on and go on, printing 'foo' until I kill it.<br></div><div class="gmail_default" style="font-family:tahoma,sans-serif;color:rgb(0,0,0)"><br></div><div class="gmail_default" style="font-family:tahoma,sans-serif;color:rgb(0,0,0)">Please comment, explain or recommend this further if you also think that both while statements should behave identically.</div><div class="gmail_default" style="font-family:tahoma,sans-serif;color:rgb(0,0,0)"><br></div><div class="gmail_default" style="font-family:tahoma,sans-serif;color:rgb(0,0,0)">hoping for responses,</div><div class="gmail_default" style="font-family:tahoma,sans-serif;color:rgb(0,0,0)">best,</div><div class="gmail_default" style="font-family:tahoma,sans-serif;color:rgb(0,0,0)">Stephan</div><div class="gmail_default" style="font-family:tahoma,sans-serif;color:rgb(0,0,0)"><br></div><div class="gmail_default" style="font-family:tahoma,sans-serif;color:rgb(0,0,0)"><br></div></div>
<br></div></div>_______________________________________________<br>
Python-ideas mailing list<br>
<a href="mailto:Python-ideas@python.org" target="_blank">Python-ideas@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/python-ideas" rel="noreferrer" target="_blank">https://mail.python.org/mailman/listinfo/python-ideas</a><br>
Code of Conduct: <a href="http://python.org/psf/codeofconduct/" rel="noreferrer" target="_blank">http://python.org/psf/codeofconduct/</a><br></blockquote></div><br></div>
</blockquote></div><br></div></div>