Testing for empty iterators?

Roy Smith roy at panix.com
Sat Jul 3 14:06:01 EDT 2004


In the old days, if I wanted to return a sequence of items, I'd return a 
list, and loop over it like this:

for thing in getListOfThings ():
   do something

With iterators, I'm doing:

for thing in getThingIterator ():
   do something.

Now I need to test to see if the iterator is empty.  Actually, it's a 
unit test where I want to assert that it is empty. In the old days, I 
would have done:

assertEquals (getListOfThings (), [])

but I don't see any clean way to do this with an iterator.  The best I 
can come up with is something like:

flag = False
for thing in getThingIterator ():
   flag = True
   break
assertEquals (flag, False)

Is that really the only way to do it?



More information about the Python-list mailing list