# Copy operator (was list.remove for Noivces)

Mark Fardal fardal at coral.phys.uvic.ca
Thu Nov 29 02:31:22 CET 2001

```Chris Barker <chrishbarker at home.net> writes:
> ...

> Here is a little example:
> ... nice example snipped ...

>From a newbie, thanks.  I would sum this up with "a Python variable
name is a reference; a list is a series of references".
Why isn't this sort of example prominent in, say, the tutorial?

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...

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

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])

python> a = arange(4)
python> a
array([0, 1, 2, 3])
python> import copy
python> b = copy.copy(a)
python> a[0] = 666
python> b
array([0, 1, 2, 3])

This crucial difference in the behavior of the [:] operator seems
to be inadequately explained in the Numeric manual, which just says:

In other words, [:] with no arguments is the same as [:] for lists -
it can be read "all indices along this axis".

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.  Apparently alist.append(7) modifies the old object.
How about alist = alist * 2?  Looks like a new object.  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...

> What I get from this is that someone using Python had better understand
> how Python name binding, mutable types, copying, and deep copying work
> in order to not get bit eventually. ...
>
> -Chris

Absolutely. The introductions to the language that I've seen so far
(Tutorial, Numeric documentation, FAQ, Andre Lessa's book) do an poor
job of explaining these issues, in my opinion.  The information is
either not there or not stressed enough.  Which is why you guys have
to keep explaining them to newbies.

thanks,
Mark Fardal
University of Victoria

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

```