Turning f(callback) into a generator

Diez B. Roggisch deets_noospaam at web.de
Wed Dec 3 15:53:00 EST 2003


> def path_gen(start):
>     res = []
>     def cb(r, dir, names):
>         for n in names:
>             r.append(n)
> 
>     os.path.walk(start, cb, res)
>     for n in res:
>         yield n
> 
> 
> g = path_gen("/etc")
> 
> for n in g:
>     print n

Just found out that lists support extend, which allows the ugly loop for
appending names in cb to be written this way:

r.extend(names)

Diez




More information about the Python-list mailing list