extract method with generators

Cameron Simpson cs at zip.com.au
Thu Oct 14 23:45:35 EDT 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:
  http://www.python.org/dev/peps/pep-0380/
Short answer, not available yet.

A Google search on:

  python pep "yield from"

found some implementations at activestate, such as this:

  http://code.activestate.com/recipes/577153-yet-another-python-implementation-of-pep-380-yield/

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

  yield _from(gen())

where gen() is the decorated generator.

Cheers,
-- 
Cameron Simpson <cs at zip.com.au> DoD#743
http://www.cskk.ezoshosting.com/cs/

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