extract method with generators

Cameron Simpson cs at zip.com.au
Fri Oct 15 05:45:35 CEST 2010

On 14Oct2010 20:11, Steve Howell <showell30 at yahoo.com> wrote:
| Is there a way to extract code out of a generator function f() into
| g() and be able to have f() yield g()'s result without this idiom?:
|   for g_result in g():
|     yield g_result
| It feels like a clumsy hindrance to refactoring, to have to introduce
| a local variable and a loop.

This sounds like the "yield from" proposal that had discussion some
months ago. Your above idiom would become:

  yield from g()

See PEP 380:
Short answer, not available yet.

A Google search on:

  python pep "yield from"

found some implementations at activestate, such as this:


which lets you decorate an existing generator so that you can write:

  yield _from(gen())

where gen() is the decorated generator.

Cameron Simpson <cs at zip.com.au> DoD#743

What I want is Facts. Teach these boys and girls nothing but Facts.  Facts
alone are wanted in life. Plant nothing else, and root out everything else.
        - Charles Dickens    John Huffam   1812-1870  Hard Times [1854]

More information about the Python-list mailing list