0

Let's make it a bit easier.
Code:

level3 = {'a':'aa'}                                                             
level2 = {'b':level3, 'd':level3}                                               
level1 = {'j':level2, 'k':level2}                                               

def print_rec(node = None):                                                 
    if node is None:                                                            
        node = level1                                                           
    if node == 'aa':                                                            
        return                                                                  
    for key, successor in node.items():                                         
        print(key,":",node.get(key))                                            
        print_rec(successor)                                                

print_rec()

Outputs:

k : {'d': {'a': 'aa'}, 'b': {'a': 'aa'}}
d : {'a': 'aa'}
a : aa
b : None
Traceback (most recent call last):
  File "test.py", line 13, in <module>
    print_rec(level1)
  File "test.py", line 11, in print_rec
    print_rec(node)
  File "test.py", line 11, in print_rec
    print_rec(node)
  File "test.py", line 8, in print_rec
    for key in node:
TypeError: 'NoneType' object is not iterable

I think node = node.get(key) will be executed only if the key is in node. So why the new node will get a NoneType? Anyone can help?

1 Answer 1

4

In the for loop, it seems like you're using the name node for two different things:

for key in node:                                                            
    print(key,":",node.get(key))                                            
    node = node.get(key)                                                    
    print_rec(node)

When in the first iteration, you change the value of node. In the second iteration, when you do node.get(key), you're using the new node, but you want to be using the original node.

This should help:

for key in node:
    print(key,":",node.get(key))
    successor = node.get(key)
    print_rec(successor)

It can be written even more concisely like this:

for key, successor in node.items():
    print(key,":",successor)
    print_rec(successor)
Sign up to request clarification or add additional context in comments.

5 Comments

Thank you, it works. Could you show me why "node = node.get(key)" will lead to an unexpected result? If the copy is shallow, can I regard the "node" as a pointer? If so, "node = node.get(key)" will be fine, I think.
I've added an explanation.
Thanks. I modified it with a default argument so that when I encapsulate this method in a class, user can call it without accessing private field. I wonder if there more concise way to achieve this?
And under some situation, if recursion returns with a None, my way may lead to an infinite loop, I think.
I don't understand what you're asking. If you want help on this, simply create a new question :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.