If I have a list x = [1, 2, 3]
and pass it to a function f
which uses the operator +=
in the form f(x[:])
, a shallow copy is made and the contents are unchanged:
def f(x):
print "f, x = ", x, ", id(x) = ", id(x)
x += [1]
print "f, x = ", x, ", id(x) = ", id(x)
x = [1,2,3]
print "x = ", x, ", id(x) = ", id(x)
f(x[:])
print "x = ", x, ", id(x) = ", id(x)
Output:
x = [1, 2, 3] , id(x) = 139701418688384
f, x = [1, 2, 3] , id(x) = 139701418790136
f, x = [1, 2, 3, 1] , id(x) = 139701418790136
x = [1, 2, 3] , id(x) = 139701418688384
However, expecting the same behavior for an ndarray
x = np.array([1, 2, 3])
I was surprised that the contents were changed, even though a copy was indeed made:
import numpy as np
def f(x):
print "f, x = ", x, ", id(x) = ", id(x)
x += [1]
print "f, x = ", x, ", id(x) = ", id(x)
x = np.array([1,2,3])
print "x = ", x, ", id(x) = ", id(x)
f(x[:])
print "x = ", x, ", id(x) = ", id(x)
Output:
x = [1 2 3] , id(x) = 139701418284416
f, x = [1 2 3] , id(x) = 139701418325856
f, x = [2 3 4] , id(x) = 139701418325856
x = [2 3 4] , id(x) = 139701418284416
(I know the +[1]
function acts differently for an ndarray vs a list). How can I pass an ndarray like the list and avoid this behavior?
Bonus question Why is the problem resolved by using x = x + [1]
in the function f
?