<html><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><div><div><div>On Aug 28, 2008, at 10:19 PM, C. Titus Brown wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite"><div>On Thu, Aug 28, 2008 at 10:13:48PM -0400, Edward Waller wrote:<br>-> So from wikipedia I see that<br>-> <br>-> Reentrant: A computer program or routine is described as reentrant if &nbsp;<br>-> it can be safely executed concurrently; that is, the routine can be re- <br>-> entered while it is already running. To be reentrant, a function must:<br>-> <br>-> Hold no static (global) non-constant data.<br>-> Must not return the address to static (global) non-constant data.<br>-> Must work only on the data provided to it by the caller.<br>-> Must not rely on locks to singleton resources.<br>-> Must not call non-reentrant functions.<br>-> <br>-> Since def next(self): takes no arguments, how exactly are we supposed &nbsp;<br>-> to make this reentrant? &nbsp;The only thing I can think of is making a new &nbsp;<br>-> instance for the iterator like:<br>-> <br>-> class ReentrantFibonacciIterator(FibonacciIterator, object):<br>-> &nbsp;&nbsp;&nbsp;def __iter__(self):<br>-> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return ReentrantFibonacciIterator(self.n)<br>-> <br>-> But i'm not sure if i'm the right track with that, it doesn't seem &nbsp;<br>-> like you're testing this?<br>-> <br>-> Anyway if anyone has any ideas feel free to share =].<br><br>Hi Edward,<br><br>you're on the right track. &nbsp;The key tests are these, of course:<br><br>---<br>x = homework1.FibonacciIterator(8)<br>assert list(x) == fib8<br>assert list(x) == []<br><br>x = homework1.ReentrantFibonacciIterator(8)<br>assert list(x) == list(x)<br>assert list(x) == fib8<br>---<br><br>An equivalent way to state things is this: when I do<br><br>x = homework1.FibonacciIterator(8)<br>for a in x:<br> &nbsp;for b in x:<br> &nbsp;&nbsp;&nbsp;&nbsp;print a, b<br><br>I would expect to see<br><br>0 0<br>0 1<br>0 1<br>0 2<br>0 3<br>0 5<br>...<br><br>for a re-entrant iterator, and<br><br>0 1<br>0 2<br>0 3<br>0 5<br>0 8<br>...<br><br>for a non-re-entrant one.<br><br>And note that 'next(self)' does indeed take *one* argument... &nbsp;And that<br>there's no requirement that __iter__ return self.<br><br>OK, enough hints. &nbsp;Go make it work :)<br><br>cheers,<br>--titus<br>-- <br>C. Titus Brown, <a href="mailto:ctb@msu.edu">ctb@msu.edu</a><br><br></div></blockquote></div><br></div><div>Okay, since the sequence is&nbsp;[0, 1, 1, 2, 3, 5, 8, 13, 21], shouldn't that code:</div><div><br></div><div><blockquote type="cite">x = homework1.FibonacciIterator(8)<br>for a in x:<br>&nbsp;for b in x:<br>&nbsp;&nbsp;&nbsp;&nbsp;print a, b</blockquote><br></div><div>store 'a' as the first number in the sequence, then continue with b for the 2nd to last so you get:</div><div><br></div><div><span class="Apple-style-span" style="font-family: LuxiMono; ">0 1<br style="margin-top: 0px; ">0 1<br>0 2<br>0 3<br>0 5<br>0 8<br>0 13<br>0 21</span></div><div><font class="Apple-style-span" face="LuxiMono"><br></font></div><div><font class="Apple-style-span" face="LuxiMono">with two 1s to start?</font></div><div><font class="Apple-style-span" face="LuxiMono"><br></font></div><div><font class="Apple-style-span" face="LuxiMono">Anyway, the reason I asked about the tests was because the way you worded it</font></div><div><font class="Apple-style-span" face="LuxiMono"><br></font></div><div><font class="Apple-style-span" face="LuxiMono">"ReentrantFibonacciIterator(n) - a re-entrant version of the fib iterator,</font></div><div><font class="Apple-style-span" face="LuxiMono">&nbsp;&nbsp; &nbsp;i.e. something that can be iterated over multiple times."</font></div><div><font class="Apple-style-span" face="LuxiMono"><br></font></div><div><font class="Apple-style-span" face="LuxiMono">and the way you tested it made it seem like it was just an iterator that reset itself when it reached the end. &nbsp;But thanks for the clarification =].</font></div></body></html>