Copy operator (was list.remove for Noivces)

Mark Fardal fardal at coral.phys.uvic.ca
Thu Nov 29 19:15:24 EST 2001


Chris Barker <chrishbarker at home.net> writes:

> A slice of an array is a reference. This is different than a list. Use:
> 
> b = a.copy()

Not available in Python 1.5.2, as you feared.  Does it behave differently 
than b = copy.copy(a) ?

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

Hm, the quoted criterion seems more than slightly circular to me.  
How does it tell you that copy.copy(a) makes a copy of the data, but 
a[:] does not?

> I would
> suggest posting a note to the NumPy list, suggesting an enhancment to
> the slicing section of the docs.

Will do.  

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

Okay.  Again I think this could be stressed more in the docs.
Especially since "augmented assignment" is the official name for
operators like *= , and...

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

thanks
Mark



More information about the Python-list mailing list