Testing for empty iterators?

Michele Simionato michele.simionato at gmail.com
Sun Jul 4 10:58:36 CEST 2004


Roy Smith <roy at panix.com> wrote in message news:<roy-0D409A.14060003072004 at reader2.panix.com>...
> 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?

You may want to check this thread:

http://groups.google.it/groups?q=empty+iterators+group:comp.lang.python.*&hl=it&lr=&ie=UTF-8&group=comp.lang.python.*&selm=2259b0e2.0307210626.11a1bbf1%40posting.google.com&rnum=1

 Michele Simionato



More information about the Python-list mailing list