I know when send a list as parameter to a function, the changes in function will change the list, for example:
p=[[3, 4], [2, 3]]
node=[5,3]
def foo(node, p_as_list):
p_as_list.append(node)
foo(node, p)
print(p) #p changes
to stop the changes I used copy():
p=[[3, 4], [2, 3]]
node=[5,3]
def foo(node, p_as_list):
p_as_list.append(node)
#this line
foo(node, p.copy())
print(p) # without changes
but if I change the function like this:
p=[[3, 4], [2, 3]]
node=[5,3]
def foo(node, p_as_list):
#this line
p_as_list=p_as_list[-1]
p_as_list.append(node)
foo(node, p.copy())
print(p)
p will change again, I cant understand why this happen ?