I am trying to write a node class to create a tree structure. There seems to be some problem when I'm trying to add a child node to the root node with the method "addChild" because the child node appears to have itself in its children list. I couldn't figure out why, so any help is appreciated.
class node(object):
def __init__(self, name, x, y, children = []):
self.name = name
self.children = children
self.x = x
self.y = y
def addChild(self):
b=node('b', 5, 5)
self.children.append(b)
return
root=node('a',0.,0.)
print root.children
root.addChild()
print root.children
print root.children[0].children
Yields:
[<__main__.node object at 0x7faca9f93e50>]
[<__main__.node object at 0x7faca9f93e50>]
Whereas the second "print" line should have returned an empty array.