Possible Duplicate:
What is the difference between LIST.append(1) and LIST = LIST + [1] (Python)
I have a doubt on how parameters are passed to functions and their mutability, especially in the case of lists.
Consider the following…
def add_list(p):
p = p + [1]
def append_list(p):
p.append(1)
p = [1, 2, 3]
add_list(p)
print p
append_list(p)
print p
The output I get is…
[1, 2, 3]
[1, 2, 3, 1]
Why does the original list change when I append to it in a function, but is unchanged if I use the operator +?
Assignment operator within a function creates a new local variable.
In the *add_list* function your p is local variable.