questions regarding assignement and copy

David Cournapeau david at ar.media.kyoto-u.ac.jp
Wed Oct 18 05:59:16 CDT 2006


Hi there,

    I've just managed to nail down a bug which took me nearly two whole 
days to find: this is coming from an unexpected (at least from me) 
behaviour of numpy.
    I understand that if foo is a numpy array, doing bar = foo makes no 
copy, and whenever you change the value of foo, you change the value of bar:

    import numpy as N
    foo   = N.linspace(0, 4, 5)
    bar   = foo
    print foo[1]
    bar[1] = -1
    print foo[1]

    => prints 1 and -1.

    Now, if I do:

    bar   += 1
    print bar is foo

    prints True

    But if I do bar = bar + 1, then bar is not a copy of foo anymore. Is 
this intended ? This looks really confusing to me, and I would like to 
know what the precise rules about copy vs alias are ?

    Cheers,

    David

-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642




More information about the Numpy-discussion mailing list