[Tutor] Here is newbie doc on how to implement generators

Eric Brunson brunson at brunson.com
Fri Jul 13 16:59:55 CEST 2007


Dave Kuhlman wrote:
> I find iterators and generators fascinating.  So, in order to try
> to understand them better myself, I've written up some notes.  I'm
> hoping that these notes might help someone new to the generators
> and iterators in Python.  You can find it here:
>
>     http://www.rexx.com/~dkuhlman/python_comments.html
>     http://www.rexx.com/~dkuhlman/python_comments.html#iterators-and-generators
>
> I'll appreciate any comments and suggestions that might help me
> improve it.
>
> Please pass the above link along to anyone you think it might help.
>
> And, I have a question -- If you look at the example of the
> iterative (non-recursive) generator (the Doubler class), you will
> see that it walks a list, not a tree.  That's because I was *not*
> able to figure out how to implement a non-recursive tree walk
> generator.
>
> I found examples showing non-recursive/iterative solutions to how
> to walk a *binary* tree.  Those examples are at:
>
>     Lib/test/test_generator.py   # in the Python distribution
>     http://en.wikipedia.org/wiki/Inorder_tree_walk
>
> But, I could not find a example that shows a non-recursive way to
> walk a tree in which each node has an arbitrary number of children.
>   

If you store your tree data in an adjacency list iteration becomes trivial.

> Alas, I could not write that non-recursive tree walk.  The recusive
> walk is easy and clean.  So, maybe I should not worry about the
> non-recursive approach.  Still, it really bothers me that I could
> not do it.
>
> So, if you know where there are some examples, that would help me
> improve my notes on iterators and generators, please give me a
> link.
>
> Dave
>
>
>
>   



More information about the Tutor mailing list