Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #5398

Re: I don't understand generator.send()

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 <crebert@ucsd.edu>
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<BANLkTind75AkVmn9cbi9x pWetUykDL7VeA@mail.gmail.com>|Subject:=20Re:=20I=20don't =20understand=20generator.send()|From:=20Chris=20Rebert =20<crebert@ucsd.edu>|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 <crebert@ucsd.edu>
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 <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.1568.1305420495.9059.python-list@python.org> (permalink)
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

Show key headers only | View raw


On Sat, May 14, 2011 at 5:08 PM, Victor Eijkhout <see@sig.for.address> wrote:
> #! /usr/bin/env python
>
> def ints():
>    i=0
>    while True:
>        yield i
>        i += 1
>
> gen = ints()
> while True:
>    i = gen.next()
>    print i
>    if i==5:
>        r = gen.send(2)
>        print "return:",r
>    if i>10:
>        break
>
> 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 = ord(start)
...     while True:
...         sent = (yield chr(i)) # Note use of yield as expression
...         print('was sent', sent)
...         i += 1
...
>>> g = example('a')
>>> g.send(3)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

I don't understand generator.send() see@sig.for.address (Victor Eijkhout) - 2011-05-14 19:08 -0500
  Re: I don't understand generator.send() "OKB (not okblacke)" <brenNOSPAMbarn@NObrenSPAMbarn.net> - 2011-05-15 00:38 +0000
  Re: I don't understand generator.send() Chris Angelico <rosuav@gmail.com> - 2011-05-15 10:47 +1000
    Re: I don't understand generator.send() see@sig.for.address (Victor Eijkhout) - 2011-05-14 20:40 -0500
  Re: I don't understand generator.send() Chris Rebert <crebert@ucsd.edu> - 2011-05-14 17:47 -0700
  Re: I don't understand generator.send() Ian Kelly <ian.g.kelly@gmail.com> - 2011-05-14 18:57 -0600
  Re: I don't understand generator.send() Ian Kelly <ian.g.kelly@gmail.com> - 2011-05-14 19:05 -0600
  Re: I don't understand generator.send() Chris Angelico <rosuav@gmail.com> - 2011-05-15 11:17 +1000
  Re: I don't understand generator.send() Ian Kelly <ian.g.kelly@gmail.com> - 2011-05-14 21:03 -0600

csiph-web