2
class Node:                                                                     
    def __init__(self,dic_nodes = dict()):                                      
        self.dic_nodes = dic_nodes                                              

root = Node()                                                                   
print("original_root:", root)                                                   
word = "sci"                                                                    
length = len(word)                                                              
for i in range(0, length):                                                      
    if word[i] not in root.dic_nodes:                                           
        print("root:", root)                                                    
        new_node = Node()                                                       
        print("new_node:", new_node, len(new_node.dic_nodes))                   
        root.dic_nodes[word[i]] = new_node                                      
        next = root.dic_nodes[word[i]]                                          
        print("next:", next, len(root.dic_nodes[word[i]].dic_nodes))            
        root = next                                                             
        print("root:", root)                                                    
    print()

Outputs are like this:

original_root: <__main__.Node object at 0x109eb1510>  
root: <__main__.Node object at 0x109eb1510>  
new_node: <__main__.Node object at 0x109ec3350> 0  
next: <__main__.Node object at 0x109ec3350> 1  
root: <__main__.Node object at 0x109ec3350>  

root: <__main__.Node object at 0x109ec3350>  
new_node: <__main__.Node object at 0x109eb1510> 1  
next: <__main__.Node object at 0x109eb1510> 2  
root: <__main__.Node object at 0x109eb1510>  

root: <__main__.Node object at 0x109eb1510>  
new_node: <__main__.Node object at 0x109ec3fd0> 2  
next: <__main__.Node object at 0x109ec3fd0> 3  
root: <__main__.Node object at 0x109ec3fd0>

My Problem:
The 2nd time I call Node() to construct a new object, I think "len(new_node.dic_nodes)" should also be 0 just like the 1st time I create a new object. I can't figure out where my problem is.

Gabriel
  • 237
  • 3
  • 9
  • Just a note: you do not need to do `for i in range(0, len(word)): ...word[i]...`, you can simply do `for letter in word: ...letter...`. That is more _pythonic_ and cleaner/shorter. If you need index, you can still do `for index, letter in enumerate(word): ...letter...` (`index` is index, obviously). – Tadeck Aug 26 '13 at 02:20

1 Answers1

4

Because it is using the same dict as the default argument each time, the default dict grows over time. Instead do this:

class Node:                                                                     
    def __init__(self, dic_nodes=None):
        if dic_nodes is None:
            dic_nodes = dict()
        self.dic_nodes = dic_nodes    
user2357112
  • 260,549
  • 28
  • 431
  • 505
perreal
  • 94,503
  • 21
  • 155
  • 181
  • 1
    Good point about mutable default argument! +1 More available also here: [Be careful with mutable default arguments](http://stackoverflow.com/questions/101268/hidden-features-of-python#113198). – Tadeck Aug 26 '13 at 02:23