I have a python function func(A)
. A
is a numpy.array
and can be big such that I hesitate copying it over and over again. This func(A)
wants to change the content of A. I know this a python newbie issue. I used to program in C and it could be done by pointers. How can I change the content of A
so that the change is also valid outside the scope of the func(A)
?
Asked
Active
Viewed 213 times
1

Facundo Casco
- 10,065
- 8
- 42
- 63

delete_this_account
- 2,376
- 7
- 23
- 31
-
Doesn't the response to this [this][1] similar post answer your question ? [1]: http://stackoverflow.com/questions/10149416/numpy-modify-array-in-place – Joost Rekveld May 10 '12 at 21:06
-
I think I have to state that the parameter may not be a global variable. It may be a member of a class. I will try it but the real issue is taht I do not really know what is going on when I pass a variable to a function. Is it never copying the content or there is not such thing as "pass by reference" or "pass by value" in python. in C, pointers was hard to understand at first but I understood and now, this... – delete_this_account May 10 '12 at 21:19
-
This has nothing to do with globals. You can largely think of all arguments as passed by reference in Python. – Silas Ray May 10 '12 at 21:22
2 Answers
6
The tl;dr is in your case, it basically already is. Unless you assign a new value to the label A in the method, A will be a reference to the object you passed, which means any modifications to A will change the original object everywhere else you use it.
ex:
>>> def foo(A):
... print A[0]
... A[0] = 2
... print A[0]
...
>>> a = [0, 1, 2]
>>> foo(a)
0
2
>>> a
[2, 1, 2]
>>> def bar(A):
... print A[0]
... A = [0, 1, 2]
... A[0] = 2
... print A[0]
...
>>> a = [0, 1, 2]
>>> bar(a)
0
2
>>> a
[0, 1, 2]

Silas Ray
- 25,682
- 5
- 48
- 63
-
1+1. In other words, `numpy` arrays are mutable. But note that some `numpy` functions return copies; it might be worth spelling out a few basic things that are guaranteed to mutate, rather than copy, the array. (i.e. item assignment, in-place mathematical operations, etc.) – senderle May 10 '12 at 21:02
-
+1 as a C++ developer myself, it took me quite a while to wrap my mind around Python using labels as opposed to variables. There was a great page with pictures somewhere on the Internet...now where did that go? – JoeFish May 10 '12 at 21:04
-
I did not get it. How can I update the array without an assignment? The actual operation is `A=A+k*C` k is a scalar and C is a local `numpy.array` And what is tl;dr ? – delete_this_account May 10 '12 at 21:06
-
2
-
`A=A+k*C` will create a new entity that is the result of the operation and make `A` a label (roughly, a reference) to this new object. `A+=k*C` will modify the original referenced `A` value. If we say `a` is the label outside of method scope and `A` is inside, `A=A+k*C` will have no effect on `a` while `A+=k*C` will modify `a`. – Silas Ray May 10 '12 at 21:19
-
Do a little experimentation with the `is` operator doing assignments and modifications in different scopes. I think it might help you get a grasp on what's going on. – Silas Ray May 10 '12 at 21:26
-
So it is limited to in-place operations, right? How can I do `A=numpy.dot(B,C)` without allocating a new array place for A? I just want to point to the location of the origianl A. I just want pointer behavior. – delete_this_account May 10 '12 at 21:26
-
You can think of each label as a void pointer. When you do an assignment to the label, you are either assigning the pointer from another label to it or creating a new object (with requisite memory allocation etc) behind the scenes and assigning the pointer to that object to the label. You can think of accessing the labels like implicit void pointer dereferances (except unlike C, they automagically know their own type). – Silas Ray May 10 '12 at 21:35
0
You can change the value of the parameter A if the parameter you pass to A is a global parameter. The other way round would be to pass the variable from function to function. From what I understand in python, the object is not copied when you do so.

cobie
- 7,023
- 11
- 38
- 60
-
Actually, A is not a global variable, it is a member of a class and the func is a member of another class. – delete_this_account May 10 '12 at 21:03