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

2011-05-14 Thread Ian Kelly
On Sat, May 14, 2011 at 7:17 PM, Chris Angelico 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 >           sen

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

2011-05-14 Thread Victor Eijkhout
Chris Angelico wrote: > 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=0 >while True: >sent=(yield i) >if sent is not None: > yield None #

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

2011-05-14 Thread Chris Angelico
On Sun, May 15, 2011 at 11:05 AM, Ian Kelly 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

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

2011-05-14 Thread Ian Kelly
On Sat, May 14, 2011 at 6:47 PM, Chris Angelico wrote: > 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 >

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

2011-05-14 Thread Ian Kelly
On Sat, May 14, 2011 at 6:08 PM, Victor Eijkhout 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 unle

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

2011-05-14 Thread Chris Rebert
On Sat, May 14, 2011 at 5:08 PM, Victor Eijkhout wrote: > #! /usr/bin/env python > > def ints(): >    i=0 >    while True: >        yield i >        i += 1 > > gen = ints() > while True: >    i = gen.next() >    print i >    if i==5: >        r = gen.send(2) >        print "return:",r >    if i>10

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

2011-05-14 Thread Chris Angelico
On Sun, May 15, 2011 at 10:08 AM, Victor Eijkhout wrote: >        yield i >        r = 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 wha

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

2011-05-14 Thread OKB (not okblacke)
Victor Eijkhout wrote: > #! /usr/bin/env python > > def ints(): > i=0 > while True: > yield i > i += 1 > > gen = ints() > while True: > i = gen.next() > print i > if i==5: > r = gen.send(2) > print "return:",r > if i>10: > break >

I don't understand generator.send()

2011-05-14 Thread Victor Eijkhout
#! /usr/bin/env python def ints(): i=0 while True: yield i i += 1 gen = ints() while True: i = gen.next() print i if i==5: r = gen.send(2) print "return:",r if i>10: break I thought the send call would push the value "2" at the fron