O'Reilly logo
live online training icon Live Online training

Python Advanced: Generators and Coroutines

James Powell

This fast-paced class is aimed at Python programmers looking to improve their knowledge of the language with a focus on improving their ability to use Python's advanced features to build robust software systems.

This training is a deep dive into generators and coroutines in Python. This training approaches these features from a functional perspective but doesn't shy away from going deep into detail to give students a practical and comprehensive understanding of the topics.

What you'll learn-and how you can apply it

  • How Python generators work and what problems they can be used to solve.
  • How Python coroutines work and what problems they can be used to solve.

This training course is for you because...

  • You want to understand how advanced features of Python solve real problems
  • You want to improve your understanding of Python at a fundamental level
  • You want to be able to write powerful systems and libraries using these features

Prerequisites

  • Understanding of basic Python syntax
  • Understanding of basic Python OO programming

Course Set-Up:

  • Python 3.7 installed via Python.org or Anaconda
  • Curio, trio (install via pip)
  • Code files

Resources:

About your instructor

  • James Powell is a professional Python programmer and enthusiast. He started working with Python in the finance industry building reporting and analysis systems for prop trading front offices. He currently works as a consultant building data engineering and scientific computing platforms for a wide-range of clients using cutting-edge open source tools like Python and React. He is a lead technical consultant for the Telecom Infa Project, which is bringing industry-standard software engineering practices to the fields of optical and backbone engineering.

    In his spare time, he serves as a Vice President at NumFOCUS, the 501(c)3 non-profit that supports all the major tools in the Python data analysis ecosystem (incl. pandas, numpy, jupyter, matplotlib, and others.) At NumFOCUS, he helps build global open source communities for data scientists, data engineers, and business analysis. He helps NumFOCUS run the PyData conference series, and has sat on speaker selection and organizing committees for eighteen conferences. James is also a prolific speaker: since 2013, he has given over seventy conference talks at over fifty Python events worldwide.

Schedule

The timeframes are only estimates and may vary according to how the class is progressing

Generators (120 minutes)

  • the data model: iter and next (15 minutes)
  • How does Python implement generators at the language level?
  • How does this fit into the idea of Python being a protocol-driven
  • language?
  • What is the iter/next protocol?
  • the generator formulation (15 minutes)
  • How does the def/yield formulation of a generator implement the iter, next protocols?
  • comprehensions and generator expressions (15 minutes)
  • What is comprehension syntax?
  • What is a list, set, dict comprehension?
  • What is a generator expression?
  • generators as a mechanism for laziness (10 minutes)
  • What are generators useful for? Laziness-vs-eagerness.
  • generators as a mechanism for sequencing (10 minutes)
  • What are generators useful for? Enforced sequencing of operations.
  • pipeline-style programming with generators (10 minutes)
  • What are generators useful for? Pipeline-style dataflow programming.
  • itertools (10 minutes)
  • How does the itertools module help us work with iterators and generators?
  • the C API (5 minutes, if time permits)
  • What does the C-API for generators look like?

Coroutines (60 minutes)

  • the data model (15 minutes)
  • Extending the data model with send and throw.
  • "pumping" or "priming" (5 minutes)
  • Why coroutines need to be “pumped” or “primed”?
  • simplifying APIs with coroutines (15 minutes)
  • What are coroutines for? Simpler APIs using coroutines as functions with state.
  • coroutines for sequencing (10 minutes)
  • What are coroutines useful for? Sequencing?
  • building a custom async loop (15 minutes)
  • Building an async scheduler using coroutines.