Unexpected change of a function argument (array) - bug or feature?

It’s a feature.

When you type q = f you are making q point to the same array as f. So whatever changes you make to q are also visible in f. This is extremely useful, but you must be aware of it.

Whenever f is a mutable data structure, q = f followed by mutation of q will be visible in f.

If you want to avoid this, write q = copy(f) instead. (In some cases you even need q = deepcopy(f).)

BTW: read here to see how to format your code nicely: Please read: make it easier to help you

3 Likes