I can make a nested group of iterators using a recursive function like this:
def rprint(n):
for i in range(n):
print('n = %d' % n)
yield rprint(n-1)
Then for a simple example I can evaluate the nested generators manually.
>>> p3 = rprint(3)
>>> p3
<generator object rprint at 0x1043b7048>
>>> p2_0 = next(p3)
>>> p2_1 = next(p3)
>>> p2_2 = next(p3)
n = 3
n = 3
n = 3
>>> next(p3) # will raise an error
StopIteration
>>> p1_0_0 = next(p2_0)
>>> p1_0_1 = next(p2_0)
n = 2
n = 2
>>> next(p2_0)# will raise an error
StopIteration
>>> p0_0_0_0 = next(p1_0_0)
n = 1
>>> next(p1_0_0)# will raise an error
StopIteration
>>> p0_0_1_0 = next(p1_0_1)
n = 1
>>> next(p1_0_1)# will raise an error
StopIteration
and this goes on as follows ...
>>> p1_1_0 = next(p2_1)
>>> p1_1_1 = next(p2_1)
n = 2
n = 2
>>> next(p2_1)# will raise an error
StopIteration
... etc.
How can I do this in an automated way for any value of n in rprint? I am not interested in making variable references to the intermediate generators (as I did in the example to illustrate the objects structure).
prints a whole bunch before reporting that it's empty.