Copy operator (was list.remove for Noivces)

Chris Barker chrishbarker at home.net
Thu Nov 29 15:04:38 EST 2001


Mark Fardal wrote:

> But I'm still confused.  For one thing, NumPy seems to behave
> differently.  Apparently a Numeric array is not a series of
> references, but a reference to a series...

I suppose that's sort of right, but it's not that simple either. First
of all, you shouldn't be surprised that a NumPy Array is different than
a list, because is a different type of sequence a tuple is different
than a list as well.

> python> a = range(4)
> python> a
> [0, 1, 2, 3]
> python> b = a
> python> b = a[:]

# why both of these? a typo?

> python> a[0] = 666
> python> b
> [0, 1, 2, 3]

Right, a slice of a list is a copy.
 
> python> a = arange(4)
> python> a
> array([0, 1, 2, 3])
> python> b = a[:]
> python> a[0] = 666
> python> b
> array([666,   1,   2,   3])

A slice of an array is a reference. This is different than a list. Use:

b = a.copy()

If you want a copy. This is, in fact, alluded to in the docs (although
the copy method is not well documented either), but not made very clear.
In the 7th paragraph of "Array Basics" in my copy of the docs, it says:
"functions which return arrays which are simply differnt views of the
same data will in fact share their data" This applies to slices, but I
agree that that is not made clear AT ALL in that section of the docs.

The second item in the FAQ
(http://pfdubois.com/numpy/numerical_python_faq.htm) is:

Assignment doesn't make a copy?

Right. The statement y=x just creates a new name for the same array
object. While consistent with Python, this is upsetting to users of
other array languages. Likewise, x[i:j] is not a copy. Please read the
manual. 

Unfortunately, the manual really doesn't make this clear. I don't
remember how I learned it. Probably trial and error. 

One common problem with Docs for open source projects is that when you
need the beginner stuff, you aren't qualified to write it, and when you
are qualified to write it, you don't need it, and often forget what the
stumbling blocks were when you were just getting started. I would
suggest posting a note to the NumPy list, suggesting an enhancment to
the slicing section of the docs.

> Also, it is not necessarily clear to me when assignment creates a new
> object (freeing the reference to the old object), and when it modifies
> the old object.

Assignment always creates a new binding.

> Apparently alist.append(7) modifies the old object.

This is not an assignment. There is no equals sign.

> How about alist = alist * 2?  Looks like a new object.

yes, it is:
>>> alist = [1,2,3]
>>> blist = alist
>>> # we now have two bindings to the same list
...
>>> alist = alist * 2
>>> alist
[1, 2, 3, 1, 2, 3]
>>> blist
[1, 2, 3]
>>> # alist is now bound to the new list created by the multiplication, and blist is still bound to the old one.
...

What's important here is that assignment always creates a new binding.
Whether a new object is created depends on the operation. It is the "*"
in the previous example that created the new list, not the "=".

> How about
> a = 3 and then a *= 2?  (With Python 1.5.2 I don't have the *= operator.)
> I guess a is immutable so it must create a new object...

yup, and since that's the only option with an immutable object, it makes
little difference.

Note that *= (and kin) generally does not create a new object for a
mutable type:
>>> a = [1,2,3,4]
>>> b = a
>>> a *= 2
>>> a
[1, 2, 3, 4, 1, 2, 3, 4]
>>> b
[1, 2, 3, 4, 1, 2, 3, 4]

This is particularly usefull for Numeric Arrays, and is the reason that
adding the *= operators was not just syntactic sugar.

> PS: I'm using Python 1.5.2 and Numeric 15.2, if that makes a difference.

I'm not entirely sure if the array.copy() method is there in that
version. Try it out.

-Chris


-- 
Christopher Barker,
Ph.D.                                                           
ChrisHBarker at home.net                 ---           ---           ---
http://members.home.net/barkerlohmann ---@@       -----@@       -----@@
                                   ------@@@     ------@@@     ------@@@
Oil Spill Modeling                ------   @    ------   @   ------   @
Water Resources Engineering       -------      ---------     --------    
Coastal and Fluvial Hydrodynamics --------------------------------------
------------------------------------------------------------------------



More information about the Python-list mailing list