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


Groups > comp.lang.python > #5399

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

Path csiph.com!x330-a1.tempe.blueboxinc.net!feeder1.hal-mli.net!feeder.news-service.com!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!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; 'expressions.': 0.09; 'mix': 0.09; 'none:': 0.09; 'subject:()': 0.09; 'subject:don': 0.09; 'pm,': 0.11; 'this:': 0.11; 'def': 0.13; 'wrote:': 0.14; '"2"': 0.16; 'call?': 0.16; 'complicated,': 0.16; 'expression,': 0.16; 'generator.': 0.16; 'iterator': 0.16; 'object?': 0.16; 'send()': 0.16; 'yield': 0.19; 'cheers,': 0.20; 'seems': 0.21; 'code': 0.22; 'header:In-Reply-To:1': 0.22; 'calling': 0.25; 'received:209.85.161.46': 0.26; 'received:mail- fx0-f46.google.com': 0.26; "i'm": 0.26; 'instead': 0.26; 'message- id:@mail.gmail.com': 0.28; 'received:209.85.161': 0.29; 'sat,': 0.29; 'class': 0.29; 'instead,': 0.29; 'push': 0.29; 'probably': 0.30; 'behaves': 0.31; 'logic': 0.31; 'none,': 0.31; 'queue': 0.31; 'however,': 0.31; 'separate': 0.31; 'to:addr:python-list': 0.32; 'asking': 0.32; 'idea': 0.32; 'bit': 0.33; 'there': 0.35; 'correctly': 0.35; 'hoping': 0.36; 'too': 0.36; 'think': 0.36; 'none': 0.36; 'two': 0.37; 'should': 0.37; 'received:209.85': 0.37; 'received:google.com': 0.38; 'though': 0.38; 'unless': 0.38; 'to:addr:python.org': 0.39; 'where': 0.39; 'received:209': 0.39; 'how': 0.39; 'would': 0.40; "it's": 0.40; 'header:Received:5': 0.40; 'simple': 0.60; '2011': 0.62; 'view': 0.64; 'subject:skip:g 10': 0.84; 'victor': 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:content-type; bh=+La2s1tCUJz9G2WPHMpMuyrU2/qUGEHTUa539gL3P2o=; b=LwXu3gTNDQGPG/hksJmVBp3nXyp3v8c7HFJAyx582yL5la7dxFNbvI3zpE3EQIW6fA aUMCCvddT5/mQHgK2zLHnhigcTpGWXfXIqSTOOG+p1Gnmy1oPSLzL2nQw/H4UkXd0Fhn 2yMMF4aVloDIJ2p/Z5Kw4TkD/J9v2Hz+6JLgc=
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 :content-type; b=CIRJ0Xl+F0KamlKtWwvOb5v7sPTLMoOQr1Ec0H7zcsBdEkR1tgNBBh9NkHmz4l6lva Aq/+zwogsSigZjb6ESK/wDTQWSV7vJniL/gpawUc9cVbpmyIH78HyN/O1NQBXZLt66ia /LN9nx4z4ppL+gQ4xUmiu+LIpFcSlcgwSMMv0=
MIME-Version 1.0
In-Reply-To <1k19ubi.dwyvio12tkc4kN%see@sig.for.address>
References <1k19ubi.dwyvio12tkc4kN%see@sig.for.address>
From Ian Kelly <ian.g.kelly@gmail.com>
Date Sat, 14 May 2011 18:57:56 -0600
Subject Re: I don't understand generator.send()
To Python <python-list@python.org>
Content-Type text/plain; charset=ISO-8859-1
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.1569.1305421108.9059.python-list@python.org> (permalink)
Lines 50
NNTP-Posting-Host 82.94.164.166
X-Trace 1305421108 news.xs4all.nl 32470 [::ffff:82.94.164.166]:43540
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:5399

Show key headers only | View raw


On Sat, May 14, 2011 at 6:08 PM, Victor Eijkhout <see@sig.for.address> wrote:
> I thought the send call would push the value "2" at the front of the
> queue. Instead it coughs up the 2, which seems senseless to me.
>
> 1/ How should I view the send call? I'm reading the manual and dont' get
> it

There is no queue unless you create one inside the generator.  The
generator by itself behaves more like a coroutine.

> 2/ Is there a way to push something in the generator object? So that it
> becomes the next yield expression? In my code I was hoping to get
> 0,1,2,3,4,5,2,6,7 as yield expressions.

This will do what you're asking for:

def ints():
    i=0
    while True:
        next_yield = (yield i)
        while next_yield is not None:
            next_yield = (yield next_yield)
        i += 1

However, I don't think this is what you want.  The send call returns a
yield expression, which will then be the value that you just passed
in, which seems a bit silly.  Probably you want something more like
this:

def ints():
    i=0
    while True:
        next_yield = (yield i)
        while next_yield is not None:
            yield None
            next_yield = (yield next_yield)
        i += 1

Then the send() call will return None, and the next next() call will
return the value you passed in.  Note though that this is too simple
to work correctly if you call send() more than once before calling
next() again.

In general, I think it is a bad idea to mix calling next() and send()
on the same generator.  It makes the generator logic too complicated,
and I think it's better just to create a stateful iterator class
instead, where send() and next() are two entirely separate methods.

Cheers,
Ian

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