Possible duplicate:
Understanding the call style of a Python object to pass arguments to a function
I recently came across this:
x = [1,2,3]
def change_1(x):
x = x.remove(x[0])
return x
Result:
>>> change_1(x)
>>> x
[2, 3]
I find this behavior unexpected since I thought that everything inside the function does not affect external variables. In addition, I built an example where basically they do the same thing, but without using remove:
x = [1,2,3]
def change_2(x):
x = x[1:]
return x
Result:
>>> change_2(x)
[2, 3]
>>> x
[1, 2, 3]
And I get the result that I would expect, the function does not change x.
So it must be something special removethat has an effect. What's going on here?
source
share