Generators and nested functions
Peter Hansen
peter at engcorp.com
Fri Jan 25 20:14:34 EST 2002
"Edward C. Jones" wrote:
>
> The following code would be a simple way for doing a useful thing except
> that it doesn't work. Why not? Any easy fixes?
>
> from __future__ import generators
> import os
>
> def walk_dirs(startdir):
> def visit(arg, dirname, names):
> yield dirname
>
> os.path.walk(startdir, visit, None)
>
> if __name__ == '__main__':
> for dir in walk_dirs('/some/small/directory'):
> print dir
I don't know much about generators (haven't tried them
yet), but I believe that the method/function you want
turned into a generator *must* use the yield statement
somewhere. Your walk_dirs() does not use yield, but
does define a method visit() which is itself a generator.
More information about the Python-list
mailing list