How to append Class object to a list without modifying its members?
class Node:
def __init__(self, name, type, children=[]):
self.name = name
self.type = type
self.children = children
def add_child(self, child_node):
self.children.append(child_node)
def printNode(self, indent = 0):
print(self.name + " has children " + str(len(self.children)))
#for child in self.children: print(child, indent + 4)
if __name__ == "__main__":
A = Node("A", "company")
B = Node("B", "department")
B.printNode()
A.add_child(B)
B.printNode()
The append()
function adds node B to itself even though it should only add it to node A's children list, evident from output
B has children 0
B has children 1