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


Groups > comp.lang.python > #5401

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 <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; 'kelly': 0.09; 'none)': 0.09; 'none:': 0.09; 'subject:()': 0.09; 'subject:don': 0.09; 'sun,': 0.09; 'def': 0.13; 'am,': 0.14; 'wrote:': 0.14; 'none"': 0.16; 'pythonic': 0.16; 'send()': 0.16; 'work,': 0.20; 'header:In-Reply-To:1': 0.22; 'loop': 0.22; 'right.': 0.22; '(and': 0.22; 'received:209.85.214.174': 0.23; 'received:mail-iw0-f174.google.com': 0.23; 'instead': 0.26; 'chris': 0.27; 'work.': 0.27; 'message-id:@mail.gmail.com': 0.28; 'ignored.': 0.29; "won't": 0.30; 'second': 0.31; 'to:addr:python- list': 0.32; 'quite': 0.36; 'some': 0.37; 'should': 0.37; 'received:209.85': 0.37; 'received:google.com': 0.38; 'received:209.85.214': 0.39; 'to:addr:python.org': 0.39; '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:date :message-id:subject:from:to:content-type:content-transfer-encoding; bh=sYNOU5PbWKpTtn/pbxKVAZ1GViKvezAWqNnDGknY/RQ=; b=tkpoIeWdPw7MD6NIXvhKeGyQpCN7LlXetM2np7S04cuFMz+wF11ckj9dQDBhbtwROy HyfaN2jRvj96ms3ZvG/07nDTbxoljfBk0fJSJTrHXYM8TihLbvuy/uWhZMyXEib5VViI itsGcnnXw1nAtLFRKxQueuvK3JMopl33KnKR8=
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=N+FouFHGyIYxgXKj/7ZOqCUCHlpDGDuKtOueYZjwd1/G7Is+uF0Ko8fMwPMG4s72AK jCu4ByVaziIS2D26EcIWVZQYK5kVZzbQq7EXMzxO7AKxDbJJ3oV1j0CMpS4Bjnb+WQyh DUF6km4Vwh5HKIKcZZIYukYE/UAIpitZXt9ms=
MIME-Version 1.0
In-Reply-To <BANLkTi=c8mzk-n7sD=mSAPoFzfYcd5TS_A@mail.gmail.com>
References <1k19ubi.dwyvio12tkc4kN%see@sig.for.address> <BANLkTi=TWSY-Lg9ovuZEPDZWv1QXtL1UKQ@mail.gmail.com> <BANLkTi=c8mzk-n7sD=mSAPoFzfYcd5TS_A@mail.gmail.com>
Date Sun, 15 May 2011 11:17:31 +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.1571.1305422254.9059.python-list@python.org> (permalink)
Lines 26
NNTP-Posting-Host 82.94.164.166
X-Trace 1305422254 news.xs4all.nl 65870 [::ffff:82.94.164.166]:47962
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:5401

Show key headers only | View raw


On Sun, May 15, 2011 at 11:05 AM, Ian Kelly <ian.g.kelly@gmail.com> wrote:
> 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.

You're right. It needs a while loop instead of the if (and some slight
reordering):

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
       while sent is not None:
           queue.append(sent)
           sent=(yield None)  # This is the return value from gen.send()

That should work.

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