Re: [Numpy-discussion] linked variables?

2008-06-03 Thread Robert Kern
On Tue, Jun 3, 2008 at 6:25 PM, Anne Archibald
<[EMAIL PROTECTED]> wrote:
> 2008/6/3 Robert Kern <[EMAIL PROTECTED]>:
>
>> Python does not copy data when you assign something to a new variable.
>> Python simply points the new name to the same object. If you modify
>> the object using the new name, all of the other names pointing to that
>> object will see the changes. If you want a copy, you will need to
>> explicitly make one. For numpy arrays, the best way to do this is to
>> use array().
>>
>>  m_i = array(m_o)
>
> Is array() really the best way to copy an array? I would have thought
> m_i = m_o.copy() would be better - requiring less of array's clever
> guessing, and preserving subtypes.

The latter is true, but I didn't want to get into subtypes. In this
case, array's clever guessing involves recognizing that it is an
ndarray and doing the right thing. There is no recursive traversal
like with lists of lists.

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless
enigma that is made terrible by our own mad attempt to interpret it as
though it had an underlying truth."
 -- Umberto Eco
___
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://projects.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] linked variables?

2008-06-03 Thread Anne Archibald
2008/6/3 Robert Kern <[EMAIL PROTECTED]>:

> Python does not copy data when you assign something to a new variable.
> Python simply points the new name to the same object. If you modify
> the object using the new name, all of the other names pointing to that
> object will see the changes. If you want a copy, you will need to
> explicitly make one. For numpy arrays, the best way to do this is to
> use array().
>
>  m_i = array(m_o)

Is array() really the best way to copy an array? I would have thought
m_i = m_o.copy() would be better - requiring less of array's clever
guessing, and preserving subtypes. On the downside it doesn't work if
you're given a list, but perhaps np.copy() would be better (though it
loses subtype information)? Certainly it's clearer.

Anne
___
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://projects.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] linked variables?

2008-06-03 Thread Pierre GM
Payton,
In your example, use 
>>>m_o = m.copy() 
or
>>>m_o = m + 0 
or
>>>m_o = numpy.array(m, copy=True)
___
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://projects.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] linked variables?

2008-06-03 Thread Robert Kern
On Tue, Jun 3, 2008 at 4:19 PM, Payton Gardner <[EMAIL PROTECTED]> wrote:
> 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?

Python does not copy data when you assign something to a new variable.
Python simply points the new name to the same object. If you modify
the object using the new name, all of the other names pointing to that
object will see the changes. If you want a copy, you will need to
explicitly make one. For numpy arrays, the best way to do this is to
use array().

  m_i = array(m_o)

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless
enigma that is made terrible by our own mad attempt to interpret it as
though it had an underlying truth."
 -- Umberto Eco
___
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://projects.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] linked variables?

2008-06-03 Thread Matthieu Brucher
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 <[EMAIL PROTECTED]>:

> 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
___
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://projects.scipy.org/mailman/listinfo/numpy-discussion