[cse491] HW1: Reentrant Iterators

C. Titus Brown ctb at msu.edu
Thu Aug 28 19:19:39 PDT 2008


On Thu, Aug 28, 2008 at 10:13:48PM -0400, Edward Waller wrote:
-> So from wikipedia I see that
-> 
-> Reentrant: A computer program or routine is described as reentrant if  
-> it can be safely executed concurrently; that is, the routine can be re- 
-> entered while it is already running. To be reentrant, a function must:
-> 
-> Hold no static (global) non-constant data.
-> Must not return the address to static (global) non-constant data.
-> Must work only on the data provided to it by the caller.
-> Must not rely on locks to singleton resources.
-> Must not call non-reentrant functions.
-> 
-> Since def next(self): takes no arguments, how exactly are we supposed  
-> to make this reentrant?  The only thing I can think of is making a new  
-> instance for the iterator like:
-> 
-> class ReentrantFibonacciIterator(FibonacciIterator, object):
->    def __iter__(self):
->      return ReentrantFibonacciIterator(self.n)
-> 
-> But i'm not sure if i'm the right track with that, it doesn't seem  
-> like you're testing this?
-> 
-> Anyway if anyone has any ideas feel free to share =].

Hi Edward,

you're on the right track.  The key tests are these, of course:

---
x = homework1.FibonacciIterator(8)
assert list(x) == fib8
assert list(x) == []

x = homework1.ReentrantFibonacciIterator(8)
assert list(x) == list(x)
assert list(x) == fib8
---

An equivalent way to state things is this: when I do

x = homework1.FibonacciIterator(8)
for a in x:
  for b in x:
     print a, b

I would expect to see

0 0
0 1
0 1
0 2
0 3
0 5
...

for a re-entrant iterator, and

0 1
0 2
0 3
0 5
0 8
...

for a non-re-entrant one.

And note that 'next(self)' does indeed take *one* argument...  And that
there's no requirement that __iter__ return self.

OK, enough hints.  Go make it work :)

cheers,
--titus
-- 
C. Titus Brown, ctb at msu.edu



More information about the cse491-fall-2008 mailing list