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


Groups > comp.lang.python > #5397

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 <rosuav@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'thread,': 0.04; 'produces': 0.07; 'empty,': 0.09; 'none:': 0.09; 'subject:()': 0.09; 'subject:don': 0.09; 'sun,': 0.09; 'this:': 0.11; 'def': 0.13; 'am,': 0.14; 'wrote:': 0.14; 'complicated,': 0.16; 'doing,': 0.16; 'expression.': 0.16; 'fifo': 0.16; 'integers.': 0.16; 'iteration.': 0.16; 'pythonic': 0.16; 'send()': 0.16; 'sequential': 0.16; 'stack': 0.16; 'yield': 0.19; 'header :In-Reply-To:1': 0.22; 'insert': 0.22; 'loop': 0.22; 'values': 0.23; 'received:209.85.214.174': 0.23; 'received:mail- iw0-f174.google.com': 0.23; 'structure': 0.24; 'example': 0.24; 'chris': 0.27; 'message-id:@mail.gmail.com': 0.28; 'changing': 0.29; 'queue': 0.31; 'to:addr:python-list': 0.32; 'url:docs': 0.33; 'change': 0.34; 'decide': 0.34; 'quite': 0.36; 'rather': 0.36; 'none': 0.36; 'supporting': 0.37; 'received:209.85': 0.37; 'url:python': 0.37; 'received:google.com': 0.38; 'but': 0.38; 'url:org': 0.38; 'lets': 0.39; 'received:209.85.214': 0.39; 'to:addr:python.org': 0.39; 'could': 0.39; 'received:209': 0.39; 'header:Received:5': 0.40; 'here:': 0.61; '2011': 0.62; 'legal': 0.64; 'url:5': 0.69; 'complexity.': 0.84; 'subject:skip:g 10': 0.84; 'victor': 0.84; 'yielded': 0.84
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:in-reply-to:references:date :message-id:subject:from:to:content-type:content-transfer-encoding; bh=jRDCeQkbN7LzNctpzeOi++KvCQ3GwhXLyiEirG+XXLM=; b=FPatp9qAPAZs2cqZnZ77d99255YSMo8JkNJBe3NtxuEvxtSJyIRu229OenKhc2QSLP +yoYr8xbOS23yiPxelb+hx8Sq5KEk69tMtOJ/1bA64HC5FjnqyB85VvI+5mDU8nM4KNX Hv7GFxko57L8T+R5XNmuJzQgfLs+bm2JvfoIc=
DomainKey-Signature a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type:content-transfer-encoding; b=u8Fr9jUKuMbZIK1t5AG70ZzmXi/o1ltk3qYZOtUU8chvVVocw1rXbuQInIRNFW25qi 3QAw/jjgNzbyvk8EjR5QJorwFDRAvgkeh+dCGH/FoQktae3TprpjwpkJy49slaQ3ncKK C9WXmMHAMwr8vOoAmwYquBtAZtG0/aH3+xYLM=
MIME-Version 1.0
In-Reply-To <1k19ubi.dwyvio12tkc4kN%see@sig.for.address>
References <1k19ubi.dwyvio12tkc4kN%see@sig.for.address>
Date Sun, 15 May 2011 10:47:09 +1000
Subject Re: I don't understand generator.send()
From Chris Angelico <rosuav@gmail.com>
To python-list@python.org
Content-Type text/plain; charset=ISO-8859-1
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.1567.1305420439.9059.python-list@python.org> (permalink)
Lines 51
NNTP-Posting-Host 82.94.164.166
X-Trace 1305420439 news.xs4all.nl 81485 [::ffff:82.94.164.166]:35611
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:5397

Show key headers only | View raw


On Sun, May 15, 2011 at 10:08 AM, Victor Eijkhout <see@sig.for.address> wrote:
>        yield i

>        r = gen.send(2)

When you send() something to a generator, it becomes the return value
of the yield expression. See the example here:
http://docs.python.org/whatsnew/2.5.html#pep-342-new-generator-features

For what you're doing, there's a little complexity. If I understand,
you want send() to be like an ungetc call... you could do that like
this:


def ints():
   i=0
   while True:
       sent=(yield i)
       if sent is not None:
          yield None  # This becomes the return value from gen.send()
          yield sent  # This is the next value yielded
       i += 1

This lets you insert at most one value per iteration. Supporting more
than one insertion is more complicated, but changing the loop
structure entirely may help:

def ints():
    i=0
    queue=[]
    while True:
        if queue:  # see other thread, this IS legal and pythonic and
quite sensible
            sent=(yield queue.pop(0))
        else:
            sent=(yield i)
            i+=1
        if sent is not None:
            yield None  # This is the return value from gen.send()
            queue.append(sent)

With this generator, you maintain a queue of sent values (if you want
it to be a LIFO stack rather than a FIFO queue, just change the pop(0)
to just pop()), and if the queue's empty, it produces sequential
integers. (Incidentally, the sent values don't have to be integers. I
leave it to you to decide whether that's any use or not.)

Hope that helps!

Chris Angelico

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