Screwing Up looping in Generator
Erik
python at lucidity.plus.com
Tue Jan 3 18:53:16 EST 2017
On 03/01/17 23:05, Deborah Swanson wrote:
> And yes, we usually used for loops for generators, unless you don't know
> when the generator will be exhausted. As in this case, where the number
> of files the generator can provide is unknown. Then we used the while
> True, break on StopIteration method.
Out of interest, *why* was it deemed necessary to do something different
if you don't know how many items the generator will generate? Was any
rationale given for that?
for x in foo:
bar(x)
... where foo is any iterable (something that has a __iter__ method
defined - including generators) will just bind each value in turn to 'x'
and will exit when the StopIteration exception is raised under the
covers by the iterator that is iterating over the iterable.
Some generators are infinite (and their iterator will never raise a
StopIteration exception).
E.
More information about the Python-list
mailing list