Popular recipes tagged "refactor"http://code.activestate.com/recipes/tags/refactor/2010-04-25T23:08:07-07:00ActiveState Code RecipesYet another Python implementation of PEP 380 (yield from) (Python)
2010-04-25T23:08:07-07:00Arnau Sanchezhttp://code.activestate.com/recipes/users/4173270/http://code.activestate.com/recipes/577153-yet-another-python-implementation-of-pep-380-yield/
<p style="color: grey">
Python
recipe 577153
by <a href="/recipes/users/4173270/">Arnau Sanchez</a>
(<a href="/recipes/tags/coroutine/">coroutine</a>, <a href="/recipes/tags/decorator/">decorator</a>, <a href="/recipes/tags/generator/">generator</a>, <a href="/recipes/tags/refactor/">refactor</a>).
Revision 3.
</p>
<p>Any Python programmer knows how extremely powerful <a href="http://www.python.org/dev/peps/pep-0255/">generators</a> are. Now (since version 2.5) Python generators can not only yield values but also receive them, so they can be used to build <a href="http://www.python.org/dev/peps/pep-0342/">coroutines</a>.</p>
<p>One drawback of the current implementation of generators is that you can only yield/receive values to/from the immediate caller. That means, basically, that you cannot easily refactor your code and write nested generators. <a href="http://www.python.org/dev/peps/pep-0380/">PEP-380</a> is the most serious effort to overcome this issue, but until it gets approved we can still play around with pure Python implementations of <em>yield from</em>. </p>
<p>This recipe follows terminology used by others in the past (<a href="http://code.activestate.com/recipes/576727">recipe566726</a>, <a href="http://code.activestate.com/recipes/576728">recipe576728</a>), but I've tried to simplify the code as much as possible.</p>