We find an interesting issue on yield
of Python. We wrote a simple program:
def F():
for i in range(5, 9):
for j in range(21, 29):
x = yield i
y = (yield j) * 100
x += y
print '>>>', x
gen = F()
next(gen)
a1 = gen.send(66)
a2 = gen.send(77)
print a1
print a2
The result is amazing:
>>> 7766
21
5
i=5, j=21 => yield i => a1=21 => send(66) => x = 66
i=5, j=21 => yield j => a2=5 => send(77) => y = 7700
print 7766
print 21
print 5
i.e. after yield i
, a1
gets the value of j
; after yield j
, a2
gets the value of i
.
Does anyone know why yield
do like this? Why not a1=5, a2=21?
next(gen)
takes the first element from the generator (i
):Then
gen.send(66)
is equal toj
(which is 21). Since your second loop is still working, subsequentgen.send(77)
is equal toi
(which is still 5).Essentially, the problem is that you consume 3 values, instead of 2.
Use
gen.send(None)
ornext(gen)
to start the generator: