Graceful handling of first line

Steven Bethard steven.bethard at
Sat Oct 9 02:54:24 CEST 2004

Josiah Carlson <jcarlson <at>> 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.  =)


More information about the Python-list mailing list