In the following code, if I use tree = parent = node(leaf_1)
the tree and parent are always having the same print result; whereas, if I change that line to tree = node(leaf_1) and parent = node(leaf_1), then the tree and parent will not be the same.
class node(object):
def __init__(self, value):
self.value = value
self.children = []
def __repr__(self, level=0):
ret = "\t"*level+repr(self.value)+"\n"
for child in self.children:
ret += child.__repr__(level+1)
return ret
def add(self, nod):
self.children.append(node(nod))
leaf_1 = [1,4,3]
leaf_2 = [2,5,3]
leaf_3 = [4,4,3]
tree = parent = node(leaf_1) #### code 1
parent.add(leaf_2)
parent.add(leaf_3)
print(tree)
print(parent)
In above code, both print have the same output as follows:
output 1:
[1, 4, 3]
[2, 5, 3]
[4, 4, 3]
However, if I change tree = parent = node(leaf_1) to tree = node(leaf_1) and parent = node(leaf_1), output:
output 2:
[1, 4, 3]
[1, 4, 3]
[2, 5, 3]
[4, 4, 3]
My question:
why the tree = parent = node(leaf_1) and tree = node(leaf_1); parent = node(leaf_1) have different output?
Thanks in advance!