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: 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: References: <1k19ubi.dwyvio12tkc4kN%see@sig.for.address> From: Ian Kelly Date: Sat, 14 May 2011 19:05:27 -0600 Subject: Re: I don't understand generator.send() To: Chris Angelico 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: 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 On Sat, May 14, 2011 at 6:47 PM, Chris Angelico wrote: > def ints(): > =A0 =A0i=3D0 > =A0 =A0queue=3D[] > =A0 =A0while True: > =A0 =A0 =A0 =A0if queue: =A0# see other thread, this IS legal and pythoni= c and > quite sensible > =A0 =A0 =A0 =A0 =A0 =A0sent=3D(yield queue.pop(0)) > =A0 =A0 =A0 =A0else: > =A0 =A0 =A0 =A0 =A0 =A0sent=3D(yield i) > =A0 =A0 =A0 =A0 =A0 =A0i+=3D1 > =A0 =A0 =A0 =A0if sent is not None: > =A0 =A0 =A0 =A0 =A0 =A0yield None =A0# This is the return value from gen.= send() > =A0 =A0 =A0 =A0 =A0 =A0queue.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.