I am reading Python Essential Reference and I am unable to understand coroutine which receive and emits return values.
Here is what the author says - "A coroutine may simultaneously receive and emit return values using yield if values are supplied in the yield expression."
Here is an example that illustrates this:
def line_splitter(delimiter=None):
print("Ready to split")
result = None
while True:
line = (yield result)
result = line.split(delimiter)
Further the author adds, In this case, we use the coroutine in the same way as before. However, now calls to send() also produce a result. For example:
>>> s = line_splitter(",")
>>> s.next()
Ready to split
>>> s.send("A,B,C")
['A', 'B', 'C' ]
>>> s.send("100,200,300")
['100', '200', '300']
I want to know how the above code works.
Thanks for any help.