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.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 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: 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 On Sun, May 15, 2011 at 10:08 AM, Victor Eijkhout wro= te: > =A0 =A0 =A0 =A0yield i > =A0 =A0 =A0 =A0r =3D 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=3D0 while True: sent=3D(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 +=3D 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=3D0 queue=3D[] while True: if queue: # see other thread, this IS legal and pythonic and quite sensible sent=3D(yield queue.pop(0)) else: sent=3D(yield i) i+=3D1 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