[Numpy-discussion] linked variables?

Matthieu Brucher matthieu.brucher@gmail....
Tue Jun 3 16:27:55 CDT 2008


Hi,

m is a variable.
m_o refers to m.
m_i refers to m_o which is m.
dummy refers to m.
dummy2 and dummy3 refer to m_o which is m.

So when you modify m_i, you are modifying the variable refered by m_i, m and
also m_o, dummy, dummy2 and dummy3.

It's always the same object, with different names. Contrary to Matlab,
Python does not copy variables, it creates references to them and copies
them only explicitely.

Matthieu

2008/6/3 Payton Gardner <w.gardner@utah.edu>:

> Its probably something simple I don't understand but...
>
> I've written a dummy function which takes an array m.  I'd like it to
> return a changed array m_i, and not change the initial array m.  I call it
> with mm = dummy(m);
>
>      3 from numpy import *;
>       4 def dummy(m):
>       5     m_o = m;
>       6     pdb.set_trace();
>       7     m_step  = 100;
>       8     m_i = m_o;
>       9     dummy = m;
>      10     dummy2 = m_o;
>      11     dummy3 = m_o;
>      12     i = 0;
>      13     m_i[i] = m_i[i] + m_step;
>      14     return(m_i);
>
> But after line 13 m, m_o, dummy, dummy2, and dummy3 are all changed by
> m_step, as well mm which is in a different name space.  All I've asked it to
> do is change m_i.  What's happening?
>
> Thanks,
> Payton
>
>
>
>
> Payton Gardner
> University of Utah
> Department of Geology and Geophysics
>
>
>
>
>
>
> _______________________________________________
> Numpy-discussion mailing list
> Numpy-discussion@scipy.org
> http://projects.scipy.org/mailman/listinfo/numpy-discussion
>
>


-- 
French PhD student
Website : http://matthieu-brucher.developpez.com/
Blogs : http://matt.eifelle.com and http://blog.developpez.com/?blog=92
LinkedIn : http://www.linkedin.com/in/matthieubrucher
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://projects.scipy.org/pipermail/numpy-discussion/attachments/20080603/77faf9d4/attachment-0001.html 


More information about the Numpy-discussion mailing list