I came across the fact that numpy arrays are passed by reference at multiple places, but then when I execute the following code, why is there a difference between the behavior of foo and bar
import numpy as np
def foo(arr):
arr = arr - 3
def bar(arr):
arr -= 3
a = np.array([3, 4, 5])
foo(a)
print a # prints [3, 4, 5]
bar(a)
print a # prints [0, 1, 2]
I’m using python 2.7 and numpy version 1.6.1
In Python, all variable names are references to values.
When Python evaluates an assignment, the right-hand side is evaluated before the left-hand side.
arr - 3creates a new array; it does not modifyarrin-place.arr = arr - 3makes the local variablearrreference this new array. It does not modify the value originally referenced byarrwhich was passed tofoo. The variable namearrsimply gets bound to the new array,arr - 3. Moreover,arris local variable name in the scope of thefoofunction. Once thefoofunction completes, there is no more reference toarrand Python is free to garbage collect the value it references. As Reti43 points out, in order forarr‘s value to affecta,foomust returnarrandamust be assigned to that value:In contrast,
arr -= 3, which Python translates into a call to the__iadd__special method, does modify the array referenced byarrin-place.