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


Groups > comp.lang.python > #5405

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 <ian.g.kelly@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.003
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'else:': 0.03; 'thread,': 0.04; 'none)': 0.09; 'none:': 0.09; 'subject:()': 0.09; 'subject:don': 0.09; 'pm,': 0.11; 'def': 0.13; 'wrote:': 0.14; 'pythonic': 0.16; 'cc:no real name:2**0': 0.20; 'cc:2**0': 0.20; 'header:In-Reply-To:1': 0.22; 'cc:addr:python-list': 0.22; 'loop': 0.22; 'right.': 0.22; '(and': 0.22; 'received:209.85.161.46': 0.26; 'received:mail-fx0-f46.google.com': 0.26; 'instead': 0.26; 'chris': 0.27; 'work.': 0.27; 'message-id:@mail.gmail.com': 0.28; 'received:209.85.161': 0.29; 'sat,': 0.29; 'class': 0.29; 'cc:addr:python.org': 0.31; 'it.': 0.31; 'import': 0.32; 'quite': 0.36; 'some': 0.37; 'self': 0.37; 'should': 0.37; 'received:209.85': 0.37; 'received:google.com': 0.38; 'but': 0.38; 'received:209': 0.39; 'header:Received:5': 0.40; '2011': 0.62; 'legal': 0.64; 'subject:skip:g 10': 0.84; 'x):': 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=Ihx5R9chh/VA6Z2UhlRTHHuPVFLV/3QDqja7TVch/KE=; b=ePGYWP7sQtlOSoMNkausFW6PreWRd925hQr9bKRdtw8F7TOeZsI0ItWOnbKztp2Vj/ MSDCc/QNbC2jlJoe3wS7duOgXi34xQQpyn6AqTKy+rUVnVqaMGz8it7OQz/YnsxgZHCx 49TQXsUSgZSJsvbmF2ZvpWRGuLpbMfGB3l6qs=
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=ln2rgEXf2gHKrG7P6FqFEeycP5bHwdWzILQLsJqleZtf0hr0H4U4Iza9OiO7f5n8Rj yr5nNIZlaInietGEtUSgBe1M5L5bi+cC/eU9l43/Tuv0dSUPd6PQ5GgeUAhc5FO3/ncx rnnp2BuvRmYQY/VMdJygNvFU5Rs7Bpt/DUPU4=
MIME-Version 1.0
In-Reply-To <BANLkTinxnzSeev8awbO7h5HW+QWeJOK88g@mail.gmail.com>
References <1k19ubi.dwyvio12tkc4kN%see@sig.for.address> <BANLkTi=TWSY-Lg9ovuZEPDZWv1QXtL1UKQ@mail.gmail.com> <BANLkTi=c8mzk-n7sD=mSAPoFzfYcd5TS_A@mail.gmail.com> <BANLkTinxnzSeev8awbO7h5HW+QWeJOK88g@mail.gmail.com>
From Ian Kelly <ian.g.kelly@gmail.com>
Date Sat, 14 May 2011 21:03:41 -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.1578.1305428652.9059.python-list@python.org> (permalink)
Lines 44
NNTP-Posting-Host 82.94.164.166
X-Trace 1305428652 news.xs4all.nl 81474 [::ffff:82.94.164.166]:39221
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:5405

Show key headers only | View raw


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

Yeah, that should do it.  But this is so much easier to get right and
to understand:

import itertools

class Ints(object):

    def __init__(self):
        self.ints = itertools.count()
        self.queue = []

    def __iter__(self):
        return self

    def next(self):
        if self.queue:
            return self.queue.pop(0)
        else:
            return self.ints.next()

    def insert(self, x):
        self.queue.append(x)

Back to comp.lang.python | Previous | NextPrevious 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