Screwing Up looping in Generator

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


On Wed, Jan 4, 2017 at 8:19 AM, Deborah Swanson
<python at deborahswanson.net> wrote:
> while True:
>   try:
>     file = files.next()
>   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.

ChrisA



More information about the Python-list mailing list