I don't understand generator.send()

Victor Eijkhout see at sig.for.address
Sat May 14 20:08:24 EDT 2011


#! /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 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
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.

Victor.


-- 
Victor Eijkhout -- eijkhout at tacc utexas edu



More information about the Python-list mailing list