1

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)?

3
  • Doesn't the response to this [this][1] similar post answer your question ? [1]: stackoverflow.com/questions/10149416/… Commented 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... Commented 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. Commented May 10, 2012 at 21:22

2 Answers 2

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]
Sign up to request clarification or add additional context in comments.

8 Comments

+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.)
+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?
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 ?
@gokhan_ufl: A += k*C will modify A in-place.
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.
|
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.

1 Comment

Actually, A is not a global variable, it is a member of a class and the func is a member of another class.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.