Screwing Up looping in Generator

Chris Angelico rosuav at
Tue Jan 3 17:30:48 EST 2017

On Wed, Jan 4, 2017 at 8:19 AM, Deborah Swanson
<python at> wrote:
> while True:
>   try:
>     file =
>   except StopIteration:
>     break

Small side point: Try to avoid calling a generator object's .next()
method directly. Normally, when you _do_ want to do this, you should
be calling next(files). In Python 3, the magic method is now
__next__(), which emphasizes that it's for defining, not calling.

As others have pointed out, though, the for loop is the correct tool
for this job.


More information about the Python-list mailing list