Graceful handling of first line

Steven Bethard steven.bethard at gmail.com
Fri Oct 8 20:54:24 EDT 2004


Josiah Carlson <jcarlson <at> uci.edu> writes:

> IMO, unconditionally breaking out of a for loop is the nicer way of
> handling things in this case, no exceptions to catch.

There's still a NameError to catch if you haven't initialized line:

>>> for line in []:
...     break
...
>>> line
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
NameError: name 'line' is not defined

I don't much like the break out of a for loop, because it feels like a misuse 
of a construct designed for iteration...  But take your pick: StopIteration or 
NameError.  =)

Steve





More information about the Python-list mailing list