<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On 16 October 2017 at 11:12, Jason Campbell <span dir="ltr"><<a href="mailto:j_campbell7@hotmail.com" target="_blank">j_campbell7@hotmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">




<div dir="ltr">
<div id="gmail-m_2962023129280358526divtagdefaultwrapper" style="font-size:12pt;color:rgb(0,0,0);font-family:Calibri,Helvetica,sans-serif" dir="ltr">
<p>I recently came across a bug where checking negative membership (__contains__ returns False) of an infinite iterator will freeze the program.</p>
<p>
</p>
<p>It may seem a bit obvious, but you can check membership in range for example without iterating over the entire range.<br></p></div></div></blockquote><div>As Terry noted, this is due to a semantic difference between range (compact representation of a tuple of integers) and arbitrary iterators.<br></div><div><br></div><div>However, if we can avoid seemingly innocent code triggering an infinite loop, that's probably a good thing. <br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div id="gmail-m_2962023129280358526divtagdefaultwrapper" style="font-size:12pt;color:rgb(0,0,0);font-family:Calibri,Helvetica,sans-serif" dir="ltr"><p>
</p>
<p>
</p>
<p>`<span>int(1e100) in range(int(1e101))`</span> on my machine takes about 1.5us<br>
`int(1e7) in itertools.count()` on my machine takes about 250ms (and gets worse quite quickly).</p>
<p>
</p>
<p>Any membership test on the infinite iterators that is negative will freeze the program as stated earlier, which is odd to me.</p></div></div></blockquote><div>The other relevant behavioural difference is that even a successful membership test will consume the iterator up to that point.</div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div id="gmail-m_2962023129280358526divtagdefaultwrapper" style="font-size:12pt;color:rgb(0,0,0);font-family:Calibri,Helvetica,sans-serif" dir="ltr"><p>
</p>
<p>itertools.count can use the same math as range</p></div></div></blockquote><div>+1, with the caveat that a successful containment test should still advance the iterator to the point immediately after the requested element, just as it does today (if people don't want that behaviour, they should be defining a suitable finite range instead).<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div id="gmail-m_2962023129280358526divtagdefaultwrapper" style="font-size:12pt;color:rgb(0,0,0);font-family:Calibri,Helvetica,sans-serif" dir="ltr">
<p>itertools.cycle could use membership from the underlying iterable</p></div></div></blockquote><div>Sort of - you'll still need to iterate through the underlying iterator at least once in order to see all of the candidate elements. However, the containment test could still be defined as running through the full cycle at most once, such that you end up either at the point immediately after the item or else back where you started (if the item wasn't found).<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div id="gmail-m_2962023129280358526divtagdefaultwrapper" style="font-size:12pt;color:rgb(0,0,0);font-family:Calibri,Helvetica,sans-serif" dir="ltr">
<p>itertools.repeat is even easier, just compare to the repeatable element</p></div></div></blockquote><div>+1 <br></div><div><br></div><div>So this sounds like a reasonable API UX improvement to me, but you'd need to ensure that you don't inadvertently change the external behaviour of *successful* containment tests.<br></div></div><div class="gmail_quote"><br></div><div class="gmail_quote">Cheers,</div><div class="gmail_quote">Nick.<br></div><br>-- <br><div class="gmail_signature">Nick Coghlan   |   <a href="mailto:ncoghlan@gmail.com" target="_blank">ncoghlan@gmail.com</a>   |   Brisbane, Australia</div>
</div></div>