Summary
This chapter showed you how to create generators and both the strengths and weaknesses that they possess. Additionally, it should now be clear how to work around their limitations and the implications of doing so.
In general, I would always recommend the use of generators over traditional collection-generating functions. They are easier to write, consume less memory, and, if needed, the downsides can be mitigated by replacing some_generator()
with list(some_generator())
, or a decorator that handles that for you.
While the paragraphs about coroutines provided some insights into what they are and how they can be used, they were just a mild introduction to coroutines. Both the pure coroutines and the coroutine generator combinations are still somewhat clunky, which is why the asyncio
library was created. Chapter 13, - asyncio – Multithreading without Threads, covers asyncio
in detail and also introduces the async
and await
statements, which make coroutine usage...