Yield in a recursive function Yield in a recursive function python python

Yield in a recursive function


Iterators do not work recursively like that. You have to re-yield each result, by replacing

explore(path)

with something like

for value in explore(path):    yield value

Python 3.3 added the syntax yield from X, as proposed in PEP 380, to serve this purpose. With it you can do this instead:

yield from explore(path)

If you're using generators as coroutines, this syntax also supports the use of generator.send() to pass values back into the recursively-invoked generators. The simple for loop above would not.


The problem is this line of code:

explore(path)

What does it do?

  • calls explore with the new path
  • explore runs, creating a generator
  • the generator is returned to the spot where explore(path) was executed . . .
  • and is discarded

Why is it discarded? It wasn't assigned to anything, it wasn't iterated over -- it was completely ignored.

If you want to do something with the results, well, you have to do something with them! ;)

The easiest way to fix your code is:

for name in explore(path):    yield name

When you are confident you understand what's going on, you'll probably want to use os.walk() instead.

Once you have migrated to Python 3.3 (assuming all works out as planned) you will be able to use the new yield from syntax and the easiest way to fix your code at that point will be:

yield from explore(path)


Use os.walk instead of reinventing the wheel.

In particular, following the examples in the library documentation, here is an untested attempt:

import osfrom os.path import joindef hellothere(somepath):    for root, dirs, files in os.walk(somepath):        for curfile in files:            yield join(root, curfile)# call and get full list of results:allfiles = [ x for x in hellothere("...") ]# iterate over results lazily:for x in hellothere("..."):    print x