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)?
-
Doesn't the response to this [this][1] similar post answer your question ? [1]: stackoverflow.com/questions/10149416/…Joost Rekveld– Joost Rekveld2012-05-10 21:06:26 +00:00Commented May 10, 2012 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– delete_this_account2012-05-10 21:19:17 +00:00Commented May 10, 2012 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– Silas Ray2012-05-10 21:22:29 +00:00Commented May 10, 2012 at 21:22
Add a comment
|
2 Answers
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]
8 Comments
senderle
+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.)JoeFish
+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?
delete_this_account
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 ?Fred Foo
@gokhan_ufl:
A += k*C will modify A in-place.Silas Ray
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. |
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.
1 Comment
delete_this_account
Actually, A is not a global variable, it is a member of a class and the func is a member of another class.