dirwalk.py generator version of os.path.walk

Just van Rossum just at xs4all.nl
Thu Feb 28 05:36:59 EST 2002

In article <a5kv49$ae8$2 at news.idiom.com>,
 jimd at vega.starshine.org (Jim Dennis) wrote:

> > I wrote a different implementation of this general concept at:
> > http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/105873
> > You don't really need to keep a stack of directories and push/pop
> > things, because with generators you can recurse instead.
> >Tom
>  But recursion is likely to cost more.  The only state I need
>  to keep is my current "todo" list of directories.  A recursion 
>  would store functional state (unless Python supported tail-end
>  recursion).  So the append/pop (total cost, 3 lines of code)
>  seems like the lightest weight way to do this.

Why don't you try it out? Setting up a generator is as expensive as 
calling a function, but resuming or yielding a generator is _very_ cheap.

I personally find the recursive version clearer.


More information about the Python-list mailing list