Syntactic structure for 'until <Exception>:' loop

eblume blume.erich at gmail.com
Tue Jan 11 23:29:06 EST 2011


On Jan 11, 6:53 pm, Ian Kelly <ian.g.ke... at gmail.com> wrote:
> On 1/11/2011 7:22 PM, eblume wrote:
>
> > <snip>
>
> reader_iter = iter(self.reader)
> headers = reader_iter.next()
> # intermediate code
> for line in reader_iter:
>      data.append(line)
> return data
>
> Also note that recommended best practice is to wrap the "headers =
> reader_iter.next()" line in a try-except in case it raises a
> StopIteration.  Otherwise it could get propagated silently up to some
> unrelated for loop higher in the stack, resulting in unexpected behavior.
>
> Cheers,
> Ian

That's brilliant, exactly the code I was looking for. Thanks very much
Ian!

Erich




More information about the Python-list mailing list