Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!bcyclone04.am1.xlned.com!bcyclone04.am1.xlned.com!newsfeed.xs4all.nl!newsfeed2a.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.005 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'made.': 0.07; '[1,': 0.09; 'iterate': 0.09; 'latter': 0.09; 'lst': 0.09; 'try:': 0.09; 'cc:addr:python-list': 0.11; 'def': 0.12; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'iterable,': 0.16; 'iterators': 0.16; 'iterators,': 0.16; 'sorts': 0.16; 'subject:generator': 0.16; 'throw': 0.16; 'true:': 0.16; 'wrote:': 0.18; '(not': 0.18; 'all,': 0.19; 'things.': 0.19; '>>>': 0.22; 'saying': 0.22; 'cc:addr:python.org': 0.22; 'cc:2**0': 0.24; '15,': 0.26; 'equivalent': 0.26; 'right.': 0.26; 'subject:/': 0.26; 'values': 0.27; 'header:In-Reply-To:1': 0.27; 'chris': 0.29; 'am,': 0.29; 'wonder': 0.29; 'message-id:@mail.gmail.com': 0.30; "i'm": 0.30; '(which': 0.31; 'bunch': 0.31; 'exceptions': 0.31; 'types.': 0.31; 'themselves': 0.32; 'quite': 0.32; 'cases': 0.33; "can't": 0.35; 'except': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'edge': 0.36; 'yield': 0.36; 'doing': 0.36; 'generic': 0.38; 'simple': 0.61; "you're": 0.61; 'skip:n 10': 0.64; 'provide': 0.64; 'more': 0.64; 'different': 0.65; 'close': 0.67; 'mar': 0.68; 'special': 0.74; '2015': 0.84; 'distinguish': 0.84; '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=6w6Ha8P2pWhRKH37OTR2r3dDiVqNyyDEM3BmQvEkRS0=; b=E+0K2ea5cDuen2OXP17KsLpyzUMTFnf5aCKMQ5ytnAA0Xg0f5GBRIg4z9zmxEnT3E6 rg+7ianGigOeb0mzbYdeC2loItv1dUOTef4XfDouuDS1S9DGlMGmC68hURsFRMaPg5Qt 6fJ6rG/CHwUrWbFyStpCQkUDJ8BGilHtMKKatIGSNmpKsC2ZjP5sqdqJ0tHFx6XBDrID AiKcKj055sbGAoFNGTK3LIXNX2aJCXQrfZk4UH4DynFs08YcbsHrPy+3tt6boq/A1P+S k8wHZar360VAxykX05F4farlRc1P8jalN7PfC5Uplscug6i+UxhF37ROc+4FMzg4mTAd ciFw== MIME-Version: 1.0 X-Received: by 10.50.9.97 with SMTP id y1mr57444516iga.34.1426384953768; Sat, 14 Mar 2015 19:02:33 -0700 (PDT) In-Reply-To: <87twxnnkzx.fsf@elektro.pacujo.net> References: <5501be8b$0$13006$c3e8da3$5496439d@news.astraweb.com> <874mpnp6nv.fsf@elektro.pacujo.net> <87y4mznmj8.fsf@elektro.pacujo.net> <87twxnnkzx.fsf@elektro.pacujo.net> Date: Sun, 15 Mar 2015 13:02:33 +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: 64 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1426384956 news.xs4all.nl 2948 [2001:888:2000:d::a6]:46697 X-Complaints-To: abuse@xs4all.nl X-Received-Bytes: 5536 X-Received-Body-CRC: 2487045174 Xref: csiph.com comp.lang.python:87462 On Sun, Mar 15, 2015 at 11:48 AM, Marko Rauhamaa wrote: > Chris Angelico : > >> On Sun, Mar 15, 2015 at 11:15 AM, Marko Rauhamaa wrote: >>> What features do generator iterators provide on top of generic >>> iterators? >> >> You can send values into them, throw exceptions into them, and close >> them (which is a special case of the latter). > > Hm. I wonder why the distinction was made. IOW, why is > > iter([1, 2, 3]) > > not equivalent with > > (x for x in [1, 2, 3]) > > I can close the latter but not the former. You don't need to close the former. > After all, > > yield from [1, 2, 3] > > works all right. That's using it as an iterable, which is not quite the same as an iterator. (Iterators are themselves iterable; iter(x) is x, for any iterator.) What you're doing there is roughly equivalent to: for x in [1, 2, 3]: yield x but with a bunch of other simplifications and guarantees for edge cases and things. So it's going to call iter() on what you give it. If you treat things as iterators, you can use all sorts of things. You don't need to distinguish the different types. The distinction is important if you want to do more than just iterate over something. >>> def gather(): lst = [] try: while True: lst.append((yield len(lst))) except StopIteration: yield lst >>> n = gather() >>> next(n) 0 >>> n.send("Hello") 1 >>> n.send("World") 2 >>> n.throw(StopIteration) ['Hello', 'World'] You can't do that with a simple iterator. (Not that I'm saying this is good design...) ChrisA