I was going through this SO question on the generator send function when I came across this code
def coroutine():
for i in range(1, 10):
print("From generator {}".format((yield i)))
c = coroutine()
c.send(None)
try:
while True:
print("From user {}".format(c.send(1)))
except StopIteration: pass
As you can see the author used braces
around the yield i
; and I don't understand why it is needed.
The doc for str.format()
doesn't mention anything about the argument to format() to be of any type. I'm sure my confusion arises from the fact that I don't know what the yield statement does or what type is generated or made when it's called.
Can you help me understand why braces are needed around the yield in a str.format function call?
The compiler wasn't very helpful and so I had to ask this question
In [1]: def coroutine():
...: for i in range(1, 10):
...: print("From generator {}".format(yield i))
...: c = coroutine()
...: c.send(None)
...: try:
...: while True:
...: print("From user {}".format(c.send(1)))
...: except StopIteration: pass
File "<ipython-input-1-024981190f27>", line 3
print("From generator {}".format(yield i))
^
SyntaxError: invalid syntax