Does anyone know how send() works with generators when used recursively? I expected the value to be passed to the current generator, which could then pass it down to the recursed-generator…but it seems that is not the case? Some example code:
def Walk(obj):
recurse = (yield obj)
if not recurse:
print 'stop recurse:', recurse
return
if isinstance(obj, list):
print 'is list:', obj
for item in obj:
print 'item loop:', item
walker = Walk(item)
for x in walker:
print 'item walk:', x
recurse = (yield x)
print 'item walk recurse:', recurse
walker.send(recurse)
root = ['a', ['b.0', ['b.0.0']]]
walker = Walk(root)
for i, x in enumerate(walker):
print i, x
print 'send true'
walker.send(True)
The desired output is should be each value at each level recursion:
0 ['a', ['b.0', ['b.0.0']]]
1 'a'
2 ['b.0', ['b.0.0']]
3 'b.0'
4 ['b.0.0']
5 'b.0.0'
What ends up happening is:
0 ['a', ['b.0', ['b.0.0']]]
send true
is list: ['a', ['b.0', ['b.0.0']]]
item loop: a
item walk: a
item walk recurse: None
stop recurse: None
It looks like the inner-loop with recurse = (yield) doesn’t wait for a value to be sent. Or something. Its not really clear how the inner-loop recurse value is getting None; its caller does call send().
Ultimately, the goal is basically to walk over a tree-structure recursively, but have the top-most caller be able to specify when not to recurse into a substructure. e.g.,
walker = Walk(root)
for node in walker:
if CriteriaMet(node):
walker.send(True)
else:
walker.send(False)
The important thing to realize is that send() also consumes!
From http://docs.python.org/reference/expressions.html#generator.send:
Here is a quick re-working of your code to get it to output as expected:
Output: