Generators and Control Abstractions in Python ============================================= I think this can be presented in 90-120 minutes. Abstract -------- In this tutorial I will present generators and related language constructs (including constructs planned for Python 2.5) with common idioms for using them. I will show how they can be used for various control abstractions (lazy computation, coroutines, simulation kernels, control inversion). Preliminary outline ------------------- * Iterators * Iterable vs. iterators * Iterator protocol, ``for`` and `iter()` * Reusing iterators: 2 fors, argv parsing, ``zip(*[iterator]*5)`` * Built-in iterators (sequences, dict, file, enumerate, reversed) * Built-in consumers (list, dict, sum, etc.) * Writing XRange and XRangeIterator classes * Generator basics * Syntax * Easy way to create iterators * Generator as an `__iter__()` method; XRange example * Do you need an iterable? Iterators as the one obvious way. * Stream processing * Avoid intermediate memory, avoid delay * Infinite streams * itertools: `imap`, `ifilter`, `izip` * itertools: other goodies * retaining values: `list`, `itertools.cycle`, `itertools.tee`, PEP 323 * Recursive generators * ``yield`` is not ``print``! * ``yield`` propogation * Example (`glob.iglob`? queens?) * Stackless flattener trick * "Generator kernels" * Idea: ``yield`` as "system call" * SimPy * MyHDL * Generator expressions * Everybody wanted dict comprehension. * `dict`, `set`, `min`/`max`, `all`/`any`, etc. * Syntax, variable scope * Producing but also consuming * Mutable arg hack * PEP 342: yield expressions * Use cases? * Coroutines * Resource release * ``try: yield finaly:`` problem * PEP 342: .throw(), .close() * __del__ discussion * Control inversion * ``for`` as a control abstraction * ``yield`` as call to loop body * ``break``/exception problem * Dirty __del__ solution * ``try: yield`` cleaner but not safer * PEP 343: ``with`` as a control abstraction * Generators as context managers