A simple generator application

Tim Peters tim.one at comcast.net
Sun Oct 13 15:59:45 EDT 2002


[Doug Fort]
> I couldn't get the generator to work with recursion. I'd be grateful
> for an example.

There's an example of a recursive tree-node generator in PEP 255.  It's also
in Lib/test/test_generators.py, in the pep_tests doctest.  They're very easy
to write once you get the hang of them, but it seems to require an "aha!"
breakthrough at the start.  It took me about 10 years to convince Guido it
was easy <wink>.

Unlike Oren, though, I prefer an explicit stack for this purpose, as, e.g.,
it's easy to switch from depth-first to breadth-first just by popping from
the other end.  Having an explicit list of pending directories also allows
easy enhancements like sorting display based on functions applied to the
directory names and/or contents.





More information about the Python-list mailing list