Modifying your function to show the id
of the objects
def fun1(x):
print(id(x),id(y))
x = 2*x
print(id(x))
return x
In [315]: y = np.arange(3)
In [316]: id(y)
Out[316]: 140296824014768
In [317]: z = fun1(y)
140296824014768 140296824014768
140296823720096
In [318]: id(z)
Out[318]: 140296823720096
So the array referenced by y
is passed to the function, and can be referenced by both x
(the argument variable) and y
(the external variable). But the assignment changes the x
reference - that object is passed back to z
. y
is unchanged.
def fun2(x):
print(id(x), id(y))
x[0] = 23
print(id(x))
return x
With this 2nd function, the assignment changes an element of x
, but doesn't change the id
of the referenced object. y
,x
and z
all reference the same array.
In [320]: y
Out[320]: array([0, 1, 2])
In [321]: id(y)
Out[321]: 140296824014768
In [322]: z = fun2(y)
140296824014768 140296824014768
140296824014768
In [323]: id(z)
Out[323]: 140296824014768
In [324]: z
Out[324]: array([23, 1, 2])
In [325]: y
Out[325]: array([23, 1, 2])
If we make a copy of y
, either before passing it to the function, or inside the function, then modifying x
will not modify y
.
In [327]: y = np.arange(3)
In [328]: id(y)
Out[328]: 140296823645328
In [329]: z = fun2(y.copy())
140296823647968 140296823645328
140296823647968
In [330]: id(z)
Out[330]: 140296823647968
In [331]: z
Out[331]: array([23, 1, 2])
In [333]: y
Out[333]: array([0, 1, 2])
The fact that we are passing the array to a function doesn't change the need for a copy. We'd get the same behavior even we just performed the action at the top level.
In [334]: y = np.arange(3)
In [335]: x = y.copy()
In [336]: x[:2]=22
In [337]: x
Out[337]: array([22, 22, 2])
In [338]: y
Out[338]: array([0, 1, 2])
We get the same behavior if the object is a list:
In [339]: yl = [1,2,3]
In [340]: fun1(yl)
140296925836360 ...
140296824729096
Out[340]: [1, 2, 3, 1, 2, 3]
In [341]: fun2(yl)
140296925836360 ...
140296925836360
Out[341]: [23, 2, 3]
In [343]: yl
Out[343]: [23, 2, 3]