Writing a class, how do I implement
foo.send(item) ?
__iter__ allows iterating over the class like a generator, what if I want it to be a coroutine?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Here is a basic example of a coroutine:
We can make a class which contains such a coroutine, and delegates calls to its
sendmethod to the coroutine:Notice that
fooacts like a coroutine (insofar as it has a send method), but is a class — it can have attributes and methods which can interact with the coroutine.For more information (and wonderful examples), see David Beazley’s Curious Course on Coroutines and Concurrency.