Dirk Paul
Dirk Paul

Reputation: 137

python passing by reference

Strange things happens today when I do things like this:

a = np.array([1, 2, 3])

b = a

b[0] = 0

print a, b

And then the value seems to be passed by reference! The answer becomes:

result: [0 2 3] [0 2 3]

But usually I think the variable in python is passed by value, like this:

a = np.array([1, 2, 3])

b = a

b = np.array([0, 2, 3])

print a, b

And then the answer becomes:

result: [1 2 3] [0 2 3]

But why is that happenning? How do I decide whether the variable is passed through reference or value? Some people said it was because of the mutable object, but I still don't quite get it. So can you explain it for me? Thanks a lot!

Upvotes: 1

Views: 123

Answers (2)

vallentin
vallentin

Reputation: 26245

As it doesn't have anything directly to do with NumPy, let's rewrite it as:

a = [1, 2, 3]

b = a

b = [0, 2, 3]

print a, b

The key thing is that names and values are separate things. So on the first line:

a = [1, 2, 3]

You create a list and assign it to a

Now on the next line you assign a to b. There is still only 1 list, both names simply refer to it.

Next you create a new list and assign it to b. Now b refers to the new list instead of the old list. In turn now only 1 name (a) refers to the other list, instead of 2 names.

Thus if we take the first example, where you do:

b[0] = 0

There both a and b reference the same list, thus the change can be observed from both names, as both names refers to the same list.

Upvotes: 3

Denziloe
Denziloe

Reputation: 8162

Variable assignment in Python is always assignment to a reference. In other words, in Python, variables are names, and assigning a variable is declaring a name for something.

a = np.array([1, 2, 3])

b = a

b[0] = 0

print a, b

This means let a be the name for that first array, then let b be a name for a, but a means the first array, so really we're just saying let b also be a name for the first array. Then b[0] = 0 means change the first element of the array. Then when you print a and b, you are printing the same thing twice (the array), because a and b are names for the same thing.

a = np.array([1, 2, 3])

b = a

b = np.array([0, 2, 3])

print a, b

The first two lines are the same as last time. a is a name for the array and so is b. Then b = np.array([0, 2, 3]) means that b is now a name for this new array. So when you print a and b you are printing a, which is still the name for the first array, and b, which is the name for the second array.

Upvotes: 1

Related Questions