code:
class Node:
def __init__(self, key, children=[]):
self.key = key
self.children = children
def __repr__(self):
return self.key
execute:
root = Node("root")
child = Node("child")
root.children.append(child)
print child.children
print root.children[0].children
result:
[child]
[child]
This is really weird, why?
Python’s version is 2.7.2.
You shouldn’t use mutable objects as default value of arguments (unless you exactly know what you’re doing). See this article for explanation.
Instead use: