[Python-ideas] Revised^4 PEP on yield-from
Adam Olsen
rhamph at gmail.com
Fri Feb 20 01:50:26 CET 2009
On Thu, Feb 19, 2009 at 5:24 PM, Greg Ewing <greg.ewing at canterbury.ac.nz> wrote:
> Traversing a binary tree with a non-generator:
>
> def traverse(node):
> if node:
> process_node(node)
> traverse(node.left)
> traverse(node.right)
>
> Traversing it with a generator:
>
> def traverse(node):
> if node:
> yield process_node(node)
> yield from traverse(node.left)
> yield from traverse(node.right)
>
> Do you still think an unrolled version would be
> equally clear? If so, you have extremely different
> tastes from me!
This is a pretty good example, IMO.
However, I'd like to see what a trampoline would look like to support
something like this:
@trampoline
def traverse(node):
if node:
yield leaf(process_node(node))
yield traverse(node.left)
yield traverse(node.right)
If the use case is sufficiently common we can consider putting such a
trampoline in the stdlib. If not it should at least go in the
cookbook.
And FWIW, a C implementation of such a trampoline should be almost
identical to what the PEP proposes. It's just substituting a type
check for the new syntax.
--
Adam Olsen, aka Rhamphoryncus
More information about the Python-ideas
mailing list