In the code below, I define a Node class which when instantiated, should have an empty "childarr" property. Then, I instantiate the class and pass it to the method, "expnd". This adds three new node to the childarr array. As expected, the first print statement shows that the length of the childarr of the "node" instance is 3. But now, I instantiate a new instance of the "Node" class from scratch. Since I specified in the init method of the class that if nothing is passed for this variable, it should be an empty array, I was expecting nn to have an empty childarr property. However, the second print statement shows it actually has three children as well.
It's very confusing to me why "nn" is being affected by some code that happened before it was instantiated and has nothing to do with it.
Does anyone know of the reason for this behavior and what I'm missing in terms of best practice?
class Node():
def __init__(self, childarr=[]):
self.childarr = childarr
def expnd(node):
for i in range(3):
newnode = Node()
node.childarr.append(newnode)
node=Node()
expnd(node)
print("Length of child array of expanded node:" + str(len(node.childarr)))
nn = Node()
print("Length of child array of new, unrelated node:" + str(len(nn.childarr)))