Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.fsmpi.rwth-aachen.de!weretis.net!feeder4.news.weretis.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 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; 'wrote': 0.14; '"b":': 0.16; 'expected,': 0.16; 'garbage': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'scope,': 0.16; 'subject:break': 0.16; 'subject:generator': 0.16; 'subject:when': 0.16; 'terminate.': 0.16; 'wrote:': 0.18; 'things.': 0.19; 'rules': 0.22; 'apply.': 0.24; 'header:X-Complaints-To:1': 0.27; 'scanned': 0.29; 'newer': 0.30; 'that.': 0.31; '"",': 0.31; '>>>>': 0.31; 'yes.': 0.31; 'file': 0.32; 'says': 0.33; '(most': 0.33; 'guess': 0.33; 'could': 0.34; 'something': 0.35; 'yield': 0.36; 'done': 0.36; 'thanks': 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; 'read': 0.60; 'break': 0.61; 'simple': 0.61; 'more': 0.64; 'here': 0.66; 'reply': 0.66; 'frank': 0.68; 'funny': 0.74; 'subject:you': 0.87 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: "Frank Millman" Subject: Re: What happens when you 'break' a generator? Date: Tue, 29 Jul 2014 09:51:06 +0200 References: X-Gmane-NNTP-Posting-Host: 197.87.161.172 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.3790.4657 X-RFC2646: Format=Flowed; Original X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.4913 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 99 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1406620295 news.xs4all.nl 2908 [2001:888:2000:d::a6]:46139 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:75346 "Peter Otten" <__peter__@web.de> wrote in message news:lr7ilg$de4$1@ger.gmane.org... > 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 "", line 1, in > File "", line 3, in f > GeneratorExit > > Thanks for the clarification, Peter. The subconscious does funny things. When I scanned your reply quickly, I could have sworn it said something about 'forcing a break'. When I read it more slowly, I could not find that. Then I realised it actually says 'for c in g' ... Frank