[Tutor] recursive generator
spir
denis.spir at gmail.com
Sun Mar 7 13:58:05 CET 2010
Hello,
Is it possible at all to have a recursive generator? I think at a iterator for a recursive data structure (here, a trie). The following code does not work: it only yields a single value. Like if child.__iter__() were never called.
def __iter__(self):
''' Iteration on (key,value) pairs. '''
print '*',
if self.holdsEntry:
yield (self.key,self.value)
for child in self.children:
print "<",
child.__iter__()
print ">",
raise StopIteration
With the debug prints in code above, "for e in t: print e" outputs:
* ('', 0)
< > < > < > < > < > < > < > < >
print len(t.children) # --> 9
Why is no child.__iter__() executed at all? I imagine this can be caused by the special feature of a generator recording current execution point. (But then, is it at all possible to have a call in a generator? Or does the issue only appear whan the callee is a generator itself?) Else, the must be an obvious error in my code.
Denis
--
________________________________
la vita e estrany
spir.wikidot.com
More information about the Tutor
mailing list