Testing for empty iterators?
Paul McGuire
ptmcg at austin.stopthespam_rr.com
Sat Jul 3 19:10:20 EDT 2004
More information about the Python-list mailing list
Sat Jul 3 19:10:20 EDT 2004
- Previous message (by thread): Testing for empty iterators?
- Next message (by thread): Testing for empty iterators?
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
"Roy Smith" <roy at panix.com> wrote in message news:roy-03678F.14110203072004 at reader2.panix.com... > I wrote: > > > flag = False > > for thing in getThingIterator (): > > flag = True > > break > > assertEquals (flag, False) > > > > Is that really the only way to do it? > > Oh, never mind, I got it... > > assertEquals (list (getThingIterator ()), []) In general, I'd say your first approach was better, or something like: try: getThingIterator.next() assert False, "getThingIterator pointed to non-empty iterable" except StopIteration: pass # no need to assert, by getting here, we know that getThingIterator pointed to an empty iterable Or if you'd prefer a more typical-looking assert: expectedNullItem = null try: expectedNullItem = getThingIterator.next() except StopIteration: pass assert expectedNullItem==null, "getThingIterator was supposed to be empty" Suppose your iterator, through some bug in your code, pointed to a list of 100,000 database records, instead of an empty list as you expected. Making a list from this iterator could be very time-consuming, when all you really needed to know was that the iterator pointed to at least one element. -- Paul
- Previous message (by thread): Testing for empty iterators?
- Next message (by thread): Testing for empty iterators?
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
More information about the Python-list mailing list