Why doesn't StopIteration get caught in the following code?

andrew cooke andrew at acooke.org
Sat Apr 4 12:05:21 EDT 2009


grocery_stocker wrote:
>>>> while True:
> ...    i = gen.next()
> ...    print i
> ...
> 0
> 1
> 4

python's magic isn't as magic as you hope.

roughly speaking, it only does the necessary rewriting (writing the
equivalent code with next etc etc) when you define a function or a method
that contains "yield".  the above doesn't, so it's not rewritten and
there's no magic.

more exactly:
http://docs.python.org/reference/datamodel.html#index-1747

andrew





More information about the Python-list mailing list