Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #75345 > unrolled thread
| Started by | Terry Reedy <tjreedy@udel.edu> |
|---|---|
| First post | 2014-07-29 03:51 -0400 |
| Last post | 2014-07-29 03:51 -0400 |
| Articles | 1 — 1 participant |
Back to article view | Back to comp.lang.python
This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by
below is the oldest one visible, not the original post.
Re: What happens when you 'break' a generator? Terry Reedy <tjreedy@udel.edu> - 2014-07-29 03:51 -0400
| From | Terry Reedy <tjreedy@udel.edu> |
|---|---|
| Date | 2014-07-29 03:51 -0400 |
| Subject | Re: What happens when you 'break' a generator? |
| Message-ID | <mailman.12411.1406620291.18130.python-list@python.org> |
On 7/29/2014 3:18 AM, Frank Millman wrote:
> Hi all
>
> Python 3.4.1
>
> Here is a simple generator -
>
> def test():
> print('start')
> for i in range(5):
> yield i
> print('done')
>
> x = test()
> for j in x:
> print(j)
>
> As expected, the output is -
>
> start
> 0
> 1
> 2
> 3
> 4
> done
>
> 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?
It sits there waiting for you to ask for the next value. Add
for j in x: print(j)
to exhaust it. Even then, it sits there until garbage collected.
--
Terry Jan Reedy
Back to top | Article view | comp.lang.python
csiph-web