[Tutor] when is a generator "smart?"

Danny Yoo dyoo at hashcollision.org
Sun Jun 2 07:32:41 CEST 2013


> So how does one access a generator one element at a time? I thought
> next would do that so I tried:
>
> print(next(uneven_squares(10,1000)))
> print(next(uneven_squares(10,1000)))
> print(next(uneven_squares(10,1000)))


Each call to uneven_squares(10, 1000) creates a fresh iterator.  You
may want to bind it so that you can reuse the same value across
multiple uses of next().

#######
us = uneven_squares(10, 1000)
print(next(us))
print(next(us))
print(next(us))
#######


More information about the Tutor mailing list