Style question on recursive generators

Carlos Ribeiro carribeiro at gmail.com
Mon Oct 18 12:52:37 EDT 2004


On Mon, 18 Oct 2004 17:42:15 +0200, Stefan Behnel
<behnel_ml at dvs1.informatik.tu-darmstadt.de> wrote:
> Carlos Ribeiro schrieb:
> > def walk(self):
> >    """generator-based recursive tree traversal"""
> >    yield child
> >    for child in childs:
> >         transfer child.walk()
> >
> > It's quite weird -- a generator plus a direct flow-of-execution
> > transfer. It's not a goto, its not a co-routine... It requires a new
> > keyword ('transfer', in this case), which _is_ a real problem --
> > something like this can't be lightly proposed. In this case, it's just
> > a thought experiment, at this point; nothing serious, and far from a
> > proposal.
> 
> 
> I know what it feels like. I had to implement the same thing for a
> recursive parser once - looks somewhat inefficient to iterate over a
> iterator only for yielding the iterator's results...
> 
> Maybe you know the itertools module?
> 
> http://www.python.org/dev/doc/devel/lib/itertools-functions.html
> 
> It has some nice functions that can make this sort of code more readable
> (and maybe even more efficient). You may find itertoold.chain especially
> useful.

Thanks -- that's a really good reference. I think I should read more
of the documentation instead of banging up my own home made solution
:-) Anyway, it does not solve the recursion problem per se, but it
gave me some ideas on own can I rewrite the code. I'll try it later...


-- 
Carlos Ribeiro
Consultoria em Projetos
blog: http://rascunhosrotos.blogspot.com
blog: http://pythonnotes.blogspot.com
mail: carribeiro at gmail.com
mail: carribeiro at yahoo.com



More information about the Python-list mailing list