as can guess title i'm not quite sure how ask question. anyway problem following:
i want pass numpy array parameter function, inside function want make copy of array, pass array function manipulating array , in end replace array copy.
... arr = np.array([...]) arr_ocp = arr.copy() func1(arr) print np.array_equal(arr,arr_ocp) -> false ... def func1(arr): arr_cp = arr.copy() func2(arr) print np.array_equal(arr,arr_cp) -> false arr = arr_cp print np.array_equal(arr,arr_cp) -> true def func2(arr): ... arr[x:x+l,y:y+w] += np.array(...) ... now did few prints , turns out in func1 arr same @ beginning , end, i'd have expected.but if print after func1(arr), arr value of arr after func2. why manipulations of func2 apply arr not arr = arr_cp?
in hindsight it's better pass copy func2 instead of array itself, still i'd not expect happen this. missing here?
you need understand difference between mutating , rebinding. when arr = arr_cp, you're doing rebinding name arr whatever referred arr_cp. don't in way affect object bound arr.
however, when arr - example, arr[0] = 'foo' - mutation, , changes actual object. names bound same object continue so, changes visible them all.
Comments
Post a Comment