I have the following function that walks a nested tree and prints the result
def walk_tree(tree):
def read_node(node):
print node
for n in node['subnodes']:
read_node(n)
read_node(tree)
If I want to return a txt with the data collected from walking the tree, thought that the following would have worked:
def walk_tree(tree):
txt = ''
def read_node(node):
txt += node
for n in node['subnodes']:
read_node(n)
read_node(tree)
But txt doesn’t seem to be in read_node‘s scope. Any suggestion?
Thanks
txtis accessible inread_node, I think it’s just some problem with+=and thattxtis not in local scope inread_node.You should use list and
"".join(...)instead ofstr += ...anyways.