Path: csiph.com!usenet.pasdenom.info!news.redatomik.org!newsfeed.xs4all.nl!newsfeed1a.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.024 X-Spam-Evidence: '*H*': 0.95; '*S*': 0.00; 'cpython': 0.05; 'generators': 0.09; 'cc:addr:python-list': 0.11; 'def': 0.12; "wouldn't": 0.14; '"with"': 0.16; '(same': 0.16; 'behave': 0.16; 'coroutines': 0.16; 'exceptions,': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'iterating': 0.16; 'iterator': 0.16; 'reason).': 0.16; 'subject:generator': 0.16; 'throw': 0.16; 'top:': 0.16; 'wrote:': 0.18; 'properly': 0.19; 'stack': 0.19; 'code,': 0.22; 'cc:addr:python.org': 0.22; 'cc:2**0': 0.24; 'source': 0.25; 'subject:/': 0.26; 'header:In-Reply-To:1': 0.27; 'raise': 0.29; 'message-id:@mail.gmail.com': 0.30; 'code': 0.31; '(on': 0.31; '13,': 0.31; 'bunch': 0.31; 'correctly.': 0.31; 'cases': 0.33; 'fri,': 0.33; 'implemented': 0.33; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'largely': 0.36; 'returning': 0.36; 'yield': 0.36; 'detail': 0.37; 'pm,': 0.38; 'rather': 0.38; 'little': 0.38; 'anything': 0.39; 'expect': 0.39; 'how': 0.40; 'even': 0.60; 'worry': 0.60; 'details.': 0.61; 'simply': 0.61; "you're": 0.61; 'more': 0.64; 'details': 0.65; 'close': 0.67; 'mar': 0.68; 'special': 0.74; '2015': 0.84; 'seldom': 0.84; 'notion': 0.91; 'whereas': 0.91; 'to:none': 0.92 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=1/FrsTJcxJllDUNeWclL7UKY1zPWFIyhkkWbkD3jP98=; b=f8mlORspmsV49scbVwo/gYBpsR/81wd7KKhMHppPFDoLfSERhQAhlXSyS+mGrJ4/nz KWzjGC3QG75oLJtzah83VYxpjpTP6YxjW1b0ZnUnpzQahGYgtQP/FPn/0lm5fAd5weW1 PCAUnYt6Wtc4qkehgSxyGReuRfoQO7JD8+V1lXpN73u45yf/wCPR9IdEjQLYxCE/yDvj 9Hmflvk7QrCIb+lrUhAHLu5bc1KQpTwgEE8r2IqBrPs1yPtvpMdInwqXAFQh28e0V9UU H2EDWvyR+CsrrDgzfxY1RBdqcGlkNrxa0C6n9KpDhwkp3EGafXUQgwng3oUOi+vpMAOR pobQ== MIME-Version: 1.0 X-Received: by 10.50.131.196 with SMTP id oo4mr82208338igb.2.1426235009684; Fri, 13 Mar 2015 01:23:29 -0700 (PDT) In-Reply-To: References: <5501be8b$0$13006$c3e8da3$5496439d@news.astraweb.com> <87twxqqewm.fsf@elektro.pacujo.net> <4eec1709-dd11-4891-bfcc-60b27bb00ee3@googlegroups.com> <550259bf$0$12975$c3e8da3$5496439d@news.astraweb.com> Date: Fri, 13 Mar 2015 19:23:29 +1100 Subject: Re: generator/coroutine terminology From: Chris Angelico Cc: "python-list@python.org" Content-Type: text/plain; charset=UTF-8 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.19 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: 34 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1426235017 news.xs4all.nl 2830 [2001:888:2000:d::a6]:41761 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:87358 On Fri, Mar 13, 2015 at 4:28 PM, Rustom Mody wrote: > And even there I would expect generators to close with StopIteration > Whereas I would expect coroutines to close (on close method) with GeneratorExit > [Ive not thought all this through properly so may be off the mark] I expect both of them to close with "return". You can throw GeneratorExit into a generator, but that's an implementation detail more than anything else (same as the throwing of SystemExist is, and for the same reason). When any iterator is exhausted, next() will raise StopIteration rather than returning something; if you're simply iterating over a generator, you can treat StopIteration as an implementation detail too: def squares_up_to(top): for n in range(top): if n*n > top: return yield n*n for sq in squares_up_to(50): print(sq) You don't need to be concerned about all those little details of resuming and stack frames and so on. You definitely don't need to worry about strange interactions around a "with" block inside the generator - you can confidently trust that everything will behave correctly. As it happens, this notion of "behaving correctly" is largely implemented using exceptions, but if it were implemented with a bunch of special cases in the CPython source code, it wouldn't matter. Write idiomatic source code and don't worry about the details. Learn how things work if you're curious, but it's seldom going to matter. ChrisA