I am using a recursive function implemented in a python class. Am I right that the local variable (passed through the methods attributes) isn't changing through a recursive change?
I have the following tree: example tree
My current method is the following:
def readSequence(self, linkTable, partTable, seq, element):
#clearFlags returns an array of objects with flags set to 0
partTable = self.clearFlags(partTable)
#readFlags returns an array of objects with updated flags. (flag > 0 if object is in sequence)
partTable = self.readFlags(partTable, seq)
#readChildren returns an array of objects with all children of the current element
children = self.readChildren(linkTable, partTable, element)
if len(children) == 0:
self.subsequences.append(seq)
else:
for child in children:
if child.flag == 1:
self.subsequences.append(seq)
return
else:
seq.append(child)
self.readSequence(linkTable, partTable, seq, child)
In my understanding the sequence seq should grow as follows:
[1][1, 2][1, 2, 4]--> appended to subsequences[1, 2, 5]--> appended to subsequences[1, 3]--> appended to subsequences
But instead it does this:
[1][1, 2][1, 2, 4]--> appended to subsequences[1, 2, 4, 5]--> appended to subsequences[1, 2, 4, 5, 3]--> appended to subsequences
The problem is clearly that the sequence seq is changed like a global variable and doesn't stay the same to add the other child.
Hope you can help me! Thanks in advance!