Very starnge problem in python

David Lowry-Duda david at lowryduda.com
Wed Feb 10 11:38:07 EST 2021


Hello!

> The very strange thing which is happening here is you see variable 
> arr2 at ID id_ttt before changing the variable tt at the same ID (i.e. 
> id_ttt]) is showing some value but once I am printing its value after 
> assigning tt[id_ttt] = 0.0, arr2[id_ttt] is also showing 0 value 
> however, I have not touched arr2 anywhere as far as changing its 
> values are concerned.

The relevant lines are these:

> def get_match(arr1, arr2, tol, itr):
>     tt = arr2

In python, this assignment doesn't make a copy, it makes `tt` and `arr2` 
refer to the same list.

You can examine this with this little code.

```
arr = [1, 2, 3]
arr_other = arr
arr_other[1] = "different"
print(arr_other) # This line prints the same
print(arr)       # array as this line.
```

Thus when you assign `tt = arr2`, modifying `tt` will modify `arr2`, and 
vice-versa.

Cheers - DLD

-- 
David Lowry-Duda <david at lowryduda.com> <davidlowryduda.com>


More information about the Python-list mailing list