Results 1 to 5 of 5

Thread: begginer confused with numpy

  1. #1
    Join Date
    Jun 2005
    Location
    Cambridge, UK
    Beans
    175

    begginer confused with numpy

    Hi, I'm a newcomer to python and want to use the numpy package for my work. I think I must still be confused about the differences between the way mutable and immutable objects are passed into functions so maybe someone can help me understand.

    As far as I know numpy arrays are mutable. Therefore why, in the following code, does the array a not change its value?

    Code:
    from numpy import *
    
    def f(x) :
        
        y = array([[1, 2],[3,4]])
        x = y.copy()
    
        return None
    
    
    a = array([[1, 1],[1,1]])
    print a
    f(a)
    print a
    Any help would be much appreciated.
    Jeremy

  2. #2
    WW is offline Iced Blended Vanilla Crème Ubuntu
    Join Date
    Oct 2004
    Beans
    1,532

    Re: begginer confused with numpy

    Your array a refers to a block of data that holds the elements of the array. After calling f, a still refers to the same block of data; f does not change that. What happens in f is that initially, x refers to the same block of data as a, but when you say "x = y.copy()", x now refers to a different block of data; you have not changed the original block of data. When f returns, a still refers to the original block of data.

    If you want the contents of a to be changed by the function f, don't reassign x to a new array; instead, changed the contents of x. For example, try this:
    Code:
    from numpy import *
    
    def f(x) :
    
        x[0,0] = 99
        x[1,1] = -1    
    
        return None
    
    
    a = array([[1, 1],[1,1]])
    print a
    f(a)
    print a

  3. #3
    Join Date
    Apr 2007
    Beans
    13

    Re: begginer confused with numpy

    Does this code clear things up at all?
    Code:
    from numpy import *
    
    def f(x) :    
        y = x.copy()
        y[0][0] = 3
    
        return None
    
    def g(x):
        y = x
        y[0][0] = 3
        return None
    
    def h(x):
        x[0][0] = 5
        return None
    
    a = array([[1, 1],[1,1]])
    print "a = \n" ,a
    f(a)
    print "after f,  a = \n", a
    g(a)
    print "after g, a = \n", a
    h(a)
    print "after h, a = \n", a
    You're probably trying to investigate the difference between functions f and g.

    So, in 'f', I create a separate copy of x. That SHOULDN'T change 'a' when I change the copy. And it doesn't.

    But, in 'g', the references are assigned.

    In your example, x and a are the same thing when you go into the function, but then you assign x to something new. It doesn't carry 'a' along. That reference is just hanging now, really. It doesn't move the things from y into x's location. It moves x to somewhere else.

    This guy takes a stab at it. . .

    http://www.goldb.org/goldblog/Commen...c4f3e8115.aspx

    On that page, it says this

    Alex is right that trying to shoehorn Python into a "pass-by-reference" or "pass-by-value" paradigm is misleading and probably not very helpful. In Python every variable assignment (even an assignment of a small integer) is an assignment of a reference. Every function call involves passing the values of those references.

  4. #4
    Join Date
    Jun 2005
    Location
    Cambridge, UK
    Beans
    175

    Re: begginer confused with numpy

    Thank you so much both of you! Starting to get the hang of it (too many years programming fortran eek)

    Jeremy

  5. #5
    Join Date
    Aug 2006
    Beans
    366

    Re: begginer confused with numpy

    I think this is what you want. You have to return the updated array, otherwise it remains local to the function and is destroyed when the function exits.
    Code:
    from numpy import *
    
    def f(x) :
        
        y = array([[1, 2],[3,4]])
        x = y.copy()
    
        return x
    
    
    a = array([[1, 1],[1,1]])
    print a
    a = f(a)
    print a
    Linux Counter entry # 99383 (since 1995), Feisty Xbuntu 64 bit
    Folders! We don't need no stinking folders. "I don't have anything on my machine that needs folding" -- Unknown

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •