When using recursion in python3 let's say I have a function 'f(a,b)'.('b' is list) And within 'f' I am calling 'f' several times recursively. If a daughter instance of 'f' makes some changes to list 'b' how do I avoid those changes in 'b' to be reflected in the calling parent f? (I am not returning 'b').
For e.g. have a look at my code below. In the second elif I am making two recursive calls to the function goToDepth. If one of the called instance makes a change to depthArr , the change is being reflected in the calling function's copy of depthArr as well which I don't want. How to avoid that?
Thanks a lot in advance!
def goToDepth(headNode,depthArr):
if(headNode==None):
return
elif(not depthArr):
return
elif(depthArr[-1]!=1):
depthArr[-1]=depthArr[-1]-1
goToDepth(headNode.left,depthArr)
goToDepth(headNode.right,depthArr)
elif (depthArr[-1]==1):
headNode.left,headNode.right=headNode.right,headNode.left
depthArr.pop()
goToDepth(headNode.left,depthArr)
goToDepth(headNode.right,depthArr)
else:
return