When i was googling for python yield
i found something interesting and I never knew before i.e we can pass value to yield to change the next() value. I hope some of the new pythonist might now aware of this and I am not sure how it works too. So, If any body could explain how it works and how the behavior changes when i send a new index to yield using send()
.
Here is my snippets :
def print_me (count):
i = 0
while i < count:
val = (yield i)
if val is not None:
i = val
else:
i += 1
gen = print_me(10)
for i in gen:
if i == 5:
gen.send(8)
else:
print i
Here is my output:
0
1
2
3
4
9