Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!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.003 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'python': 0.07; 'subject:()': 0.09; 'subject:don': 0.09; 'pm,': 0.11; '>>>': 0.12; 'def': 0.13; 'wrote:': 0.14; '"2"': 0.16; "'a'": 0.16; '/usr/bin/env': 0.16; 'call?': 0.16; 'expression.': 0.16; '\xc2\xa0i': 0.16; '\xc2\xa0if': 0.16; 'argument': 0.16; 'traceback': 0.16; '(most': 0.16; 'yield': 0.19; 'cc:no real name:2**0': 0.20; 'cc:2**0': 0.20; 'cheers,': 0.20; 'seems': 0.21; 'header:In-Reply-To:1': 0.22; 'cc:addr:python-list': 0.22; 'last):': 0.23; 'received:209.85.213': 0.23; 'statement,': 0.23; 'wonder': 0.24; 'example': 0.24; "i'm": 0.26; 'instead': 0.26; 'chris': 0.27; 'message-id:@mail.gmail.com': 0.28; 'error': 0.29; 'sat,': 0.29; 'push': 0.29; 'cc:addr:python.org': 0.31; 'typeerror:': 0.31; "can't": 0.31; 'expression': 0.33; 'things': 0.33; 'using': 0.34; 'file': 0.35; '"",': 0.35; 'received:132': 0.35; 'quite': 0.36; 'think': 0.36; 'should': 0.37; 'received:209.85': 0.37; 'received:google.com': 0.38; 'ok,': 0.39; 'received:209': 0.39; 'how': 0.39; 'would': 0.40; "it's": 0.40; 'back': 0.61; '2011': 0.62; 'view': 0.64; 'gen': 0.84; 'subject:skip:g 10': 0.84; 'victor': 0.84; 'to:none': 0.92 DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=ucsd.edu; i=crebert@ucsd.edu; q=dns/txt; s=041709-iport; t=1305420494; x=1336956494; h=mime-version:in-reply-to:references:date:message-id: subject:from:cc:content-transfer-encoding; z=MIME-Version:=201.0|In-Reply-To:=20<1k19ubi.dwyvio12tkc4 kN%see@sig.for.address>|References:=20<1k19ubi.dwyvio12tk c4kN%see@sig.for.address>|Date:=20Sat,=2014=20May=202011 =2017:47:00=20-0700|Message-ID:=20|Subject:=20Re:=20I=20don't =20understand=20generator.send()|From:=20Chris=20Rebert =20|Cc:=20python-list@python.org |Content-Transfer-Encoding:=20quoted-printable; bh=yJezeBBN1U3xZhuq/B4I+OuUjkr9bu8w2fwC9jpeEvQ=; b=BocPJgAbZmyEYgkp4bkvb+5lDwcCvC/FmHXrkORsN31Uimg/omI0WCju 2sokqwr5ANUXQcaYeskqNgo2oHTTCIJw+cgrTPd+XWl0L06vwFkOfwM+9 Oh0L9vH8FJmQWr+EY1We8f0vNyDhbFG7D7CvoFmUzrLXhgzJ2xUoLfthz 8=; X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AngFAOUhz02E7/kU/2dsb2JhbACEV6BaXAh3iHCiB4tehweIdIErg2eBBwSGUIlBhCGGZDuDVQ X-IronPort-AV: E=Sophos;i="4.64,368,1301900400"; d="scan'208";a="511670979" X-Spam-Status: No X-Spam-Level: MIME-Version: 1.0 In-Reply-To: <1k19ubi.dwyvio12tkc4kN%see@sig.for.address> References: <1k19ubi.dwyvio12tkc4kN%see@sig.for.address> Date: Sat, 14 May 2011 17:47:00 -0700 Subject: Re: I don't understand generator.send() From: Chris Rebert Cc: python-list@python.org Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 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: 63 NNTP-Posting-Host: 82.94.164.166 X-Trace: 1305420496 news.xs4all.nl 81482 [::ffff:82.94.164.166]:33241 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:5398 On Sat, May 14, 2011 at 5:08 PM, Victor Eijkhout wrot= e: > #! /usr/bin/env python > > def ints(): > =C2=A0 =C2=A0i=3D0 > =C2=A0 =C2=A0while True: > =C2=A0 =C2=A0 =C2=A0 =C2=A0yield i > =C2=A0 =C2=A0 =C2=A0 =C2=A0i +=3D 1 > > gen =3D ints() > while True: > =C2=A0 =C2=A0i =3D gen.next() > =C2=A0 =C2=A0print i > =C2=A0 =C2=A0if i=3D=3D5: > =C2=A0 =C2=A0 =C2=A0 =C2=A0r =3D gen.send(2) > =C2=A0 =C2=A0 =C2=A0 =C2=A0print "return:",r > =C2=A0 =C2=A0if i>10: > =C2=A0 =C2=A0 =C2=A0 =C2=A0break > > I thought the send call would push the value "2" at the front of the > queue. Instead it coughs up the 2, which seems senseless to me. > > 1/ How should I view the send call? I'm reading the manual and dont' get > it `yield` is an expression. Within the generator, the result of that expression is [, ignoring the complications of .throw() etc.,] the argument to .send(). You're currently using `yield` only as a statement, so it's no wonder you're not quite understanding .send(). I think this example should clarify things somewhat: >>> def example(start): ... i =3D ord(start) ... while True: ... sent =3D (yield chr(i)) # Note use of yield as expression ... print('was sent', sent) ... i +=3D 1 ... >>> g =3D example('a') >>> g.send(3) Traceback (most recent call last): File "", line 1, in TypeError: can't send non-None value to a just-started generator >>> # Ok, so we can't send something back to `yield` >>> # until we hit the first `yield`. >>> g.send(None) # Follow the error message's advice 'a' >>> g.send(3) # Let's try again now. was sent 3 'b' >>> g.send(5) was sent 5 'c' >>> g.send(9) was sent 9 'd' >>> Cheers, Chris -- http://rebertia.com