Make a generator from a recursive function

James Stroud jstroud at mbi.ucla.edu
Fri Dec 9 21:16:18 EST 2005


This was my answer to the thread "new in programing":

def do_something(*args):
   print args

def do_deeply(first, depth, lim, doit=True, *args):
   if depth < lim:
     do_deeply(first+1, depth+1, lim, False, *args)
   if first <= depth:
     do_deeply(first+1, depth, lim, True, *args + (first,))
   elif doit:
     do_something(*args)

do_deeply(first=1, depth=3, lim=4)

I thought it was a good answer, but I think better would be a generator. Is 
there a straightforward way to make such a function a generator, or does it 
require a not using a recursive function? I think "cheating" would be to 
generate the list and make an iterable from it.

Note: this is not the same as "cross" from the "N-uples from list of lists" thread.

James



More information about the Python-list mailing list