Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #75340
| Path | csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!xlned.com!feeder7.xlned.com!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <python-python-list@m.gmane.org> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.000 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'output': 0.05; 'finally:': 0.07; "subject:' ": 0.07; "'a'": 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'referenced': 0.09; 'try:': 0.09; 'python': 0.11; 'def': 0.12; '"b":': 0.16; 'expected,': 0.16; 'garbage': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'scope,': 0.16; 'subject:break': 0.16; 'subject:generator': 0.16; 'subject:when': 0.16; 'terminate.': 0.16; 'wrote:': 0.18; '>>>': 0.22; 'rules': 0.22; 'header:User-Agent:1': 0.23; 'apply.': 0.24; 'header:X -Complaints-To:1': 0.27; 'newer': 0.30; '"",': 0.31; 'yes.': 0.31; 'file': 0.32; '(most': 0.33; 'guess': 0.33; 'yield': 0.36; 'done': 0.36; 'subject:?': 0.36; 'example,': 0.37; 'to:addr:python-list': 0.38; 'recent': 0.39; 'does': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'even': 0.60; 'break': 0.61; 'simple': 0.61; 'here': 0.66; 'frank': 0.68; 'subject:you': 0.87 |
| X-Injected-Via-Gmane | http://gmane.org/ |
| To | python-list@python.org |
| From | Peter Otten <__peter__@web.de> |
| Subject | Re: What happens when you 'break' a generator? |
| Date | Tue, 29 Jul 2014 09:31:58 +0200 |
| Organization | None |
| References | <lr7hsg$3pp$1@ger.gmane.org> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset="ISO-8859-1" |
| Content-Transfer-Encoding | 7Bit |
| X-Gmane-NNTP-Posting-Host | p57bd9121.dip0.t-ipconnect.de |
| User-Agent | KNode/4.11.5 |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.15 |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list/> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.12407.1406619134.18130.python-list@python.org> (permalink) |
| Lines | 83 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1406619134 news.xs4all.nl 2923 [2001:888:2000:d::a6]:34416 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:75340 |
Show key headers only | View raw
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?
>
> 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?
Yes. In newer Pythons try...finally works, so you can see for yourself:
>>> def f():
... try:
... for c in "abc": yield c
... finally:
... print("done")
...
>>> g = f()
>>> for c in g:
... print(c)
... if c == "b": break
...
a
b
>>> del g
done
Also:
>>> g = f()
>>> next(g)
'a'
>>> g.throw(GeneratorExit)
done
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 3, in f
GeneratorExit
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: What happens when you 'break' a generator? Peter Otten <__peter__@web.de> - 2014-07-29 09:31 +0200
csiph-web