What happens when you 'break' a generator?

Johannes Schneider johannes.schneider at galileo-press.de
Tue Jul 29 03:17:46 EDT 2014


On 29.07.2014 09:18, Frank Millman wrote:

there's not print 'done' statement at the and.


> Here I break the loop -
>
> x = test()
> for j in x:
>      print(j)
>      if j == 2:
>          break
>
> Now the output is -
>
> start
> 0
> 1
> 2
>
> 'done' does not appear, so the generator does not actually terminate. What
> happens to it?
>
> My guess is that normal scoping rules apply. Using my example, the generator
> is referenced by 'x', so when 'x' goes out of scope, the generator is
> garbage collected, even though it never completed.
>
> Is this correct?
>
> Frank Millman
>
>
>


-- 
Johannes Schneider
Webentwicklung
johannes.schneider at galileo-press.de
Tel.: +49.228.42150.xxx

Galileo Press GmbH
Rheinwerkallee 4 - 53227 Bonn - Germany
Tel.: +49.228.42.150.0 (Zentrale) .77 (Fax)
http://www.galileo-press.de/

Geschäftsführer: Tomas Wehren, Ralf Kaulisch, Rainer Kaltenecker
HRB 8363 Amtsgericht Bonn



More information about the Python-list mailing list