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


Groups > comp.lang.python > #5400

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

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!selfless.tophat.at!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <ian.g.kelly@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; 'thread,': 0.04; 'produces': 0.07; 'empty,': 0.09; 'none:': 0.09; 'subject:()': 0.09; 'subject:don': 0.09; 'pm,': 0.11; 'def': 0.13; 'wrote:': 0.14; 'fifo': 0.16; 'integers.': 0.16; 'none"': 0.16; 'pythonic': 0.16; 'send()': 0.16; 'sequential': 0.16; '\xa0while': 0.16; 'stack': 0.16; 'cc:no real name:2**0': 0.20; 'cc:2**0': 0.20; 'work,': 0.20; 'header:In-Reply-To:1': 0.22; 'cc:addr:python- list': 0.22; 'values': 0.23; '\xa0if': 0.23; 'thus': 0.24; 'received:209.85.161.46': 0.26; 'received:mail- fx0-f46.google.com': 0.26; 'chris': 0.27; 'message- id:@mail.gmail.com': 0.28; 'received:209.85.161': 0.29; 'sat,': 0.29; 'ignored.': 0.29; "won't": 0.30; 'cc:addr:python.org': 0.31; 'queue': 0.31; 'second': 0.31; 'change': 0.34; 'decide': 0.34; 'quite': 0.36; 'rather': 0.36; 'none': 0.36; 'received:209.85': 0.37; 'received:google.com': 0.38; 'received:209': 0.39; 'header:Received:5': 0.40; 'twice': 0.60; '2011': 0.62; 'legal': 0.64; 'subject:skip:g 10': 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:from:date :message-id:subject:to:cc:content-type:content-transfer-encoding; bh=4OqZirD9bYaNOWNbXiZmjjXfo1kaOW2ttOWuCkAUjMQ=; b=ZFoIVE1ZLBGF4goReZJNLv1Xs+bUdVe0nJxeBsTGfNxL/uMkz4Pn9sDHP0739NF8yG SEtyMd42by6SQyUdWpiW9uP+l+oV9hqsePQ/m5jtgPBIK51XU7zF/AejAixGXP+zf/rg QO3MFVZZp4budw/UceLVUFhVRZkpqTfHI8qWw=
DomainKey-Signature a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type:content-transfer-encoding; b=g8SmHPqQDRifBvsS4YT6OsUiNllQjaAogEoPaKy+N3IsfCkMXqexUTEDRWnC7sGIX1 C288CthWMgKrPtRrhjUX35YkWaSpJRk++m05trA1juWa0nvOXj5DbVfGNLmeutQuy+O4 tFPBTxXfmyM00fi8F0Tqk71hTtzyH0pilqmPQ=
MIME-Version 1.0
In-Reply-To <BANLkTi=TWSY-Lg9ovuZEPDZWv1QXtL1UKQ@mail.gmail.com>
References <1k19ubi.dwyvio12tkc4kN%see@sig.for.address> <BANLkTi=TWSY-Lg9ovuZEPDZWv1QXtL1UKQ@mail.gmail.com>
From Ian Kelly <ian.g.kelly@gmail.com>
Date Sat, 14 May 2011 19:05:27 -0600
Subject Re: I don't understand generator.send()
To Chris Angelico <rosuav@gmail.com>
Content-Type text/plain; charset=ISO-8859-1
Content-Transfer-Encoding quoted-printable
Cc python-list@python.org
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.1570.1305421558.9059.python-list@python.org> (permalink)
Lines 27
NNTP-Posting-Host 82.94.164.166
X-Trace 1305421559 news.xs4all.nl 81484 [::ffff:82.94.164.166]:55255
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:5400

Show key headers only | View raw


On Sat, May 14, 2011 at 6:47 PM, Chris Angelico <rosuav@gmail.com> wrote:
> 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.)

Actually, this won't work, because the value of the "yield None" gets
ignored.  Thus if you try to call send() twice in a row, the generator
the treats second send() as if it were a next(), and it is not
possible to have more than one item in the queue.

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