Skip to Content
Mastering Python 2E - Second Edition
book

Mastering Python 2E - Second Edition

by Rick Hattem
May 2022
Intermediate to advanced
710 pages
16h 41m
English
Packt Publishing
Content preview from Mastering Python 2E - Second Edition

7

Generators and Coroutines – Infinity, One Step at a Time

Generator functions are functions that behave like iterators by generating the return values one by one. While traditional methods build and return a list or tuple of items with a fixed length, a generator will yield a single value only when requested by the caller. The side effect is that these generators can be infinitely large because you can keep yielding forever.

In addition to generators, there is a variation to the generator’s syntax that creates coroutines. Coroutines are functions that allow multitasking without requiring multiple threads or processes. Whereas generators can only yield values to the caller based on the initial arguments, coroutines enable two-way communication ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Mastering Object-Oriented Python - Second Edition

Mastering Object-Oriented Python - Second Edition

Steven F. Lott

Publisher Resources

ISBN: 9781800207721Supplemental Content