"for" with "else"?

Dave Harrison dave at nullcube.com
Tue Sep 30 23:50:44 EDT 2003


Andrew Dalke (adalke at mindspring.com):
> John Roth:
> > I don't see what exceptions have to do with it. There are three
> > special ending cases: with break, empty sequence and sequence
> > exhausted. Empty sequence is the only one that isn't handled easily.
> 
> Could you give an example of when you would actually use an
> 'empty_sequence' in the real world?  It's never been a problem
> for me (that I can recall) where that distinction was important.

Mmm, I think I agree with Andrew here.  In the event that you
want to specifically check whether a sequence is empty wouldn't you
want to simply make that an explicit discovery call ?

eg.

if (len(seq) > 1):
    for ...

else:
    print 'No items in sequence'

It makes more sense to me to have an explicit discovery of a lack of
items, rather than have a situation where we have the implication that
to loop over an empty sequence is incorrect.

Dave





More information about the Python-list mailing list