Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed3a.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.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'output': 0.05; "subject:' ": 0.07; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'referenced': 0.09; 'python': 0.11; 'def': 0.12; '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; 'rules': 0.22; 'apply.': 0.24; 'header:X-Complaints-To:1': 0.27; 'guess': 0.33; 'yield': 0.36; 'done': 0.36; 'subject:?': 0.36; 'example,': 0.37; 'to:addr:python-list': 0.38; '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: "Frank Millman" Subject: What happens when you 'break' a generator? Date: Tue, 29 Jul 2014 09:18:42 +0200 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: 54 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1406618335 news.xs4all.nl 2914 [2001:888:2000:d::a6]:52926 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:75337 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? Frank Millman