Elegant copy-by-value

Carl Banks imbosol at vt.edu
Tue Jan 14 03:15:07 EST 2003


Chad Netzer wrote:
> On Saturday 11 January 2003 19:51, Carl Banks wrote:
>> For example, lists (and Numeric
>> arrays) can do this:
>>
>>     a = b[:]
> 
> Just to be pedantic, Numeric arrays (ie, from the Numeric module) do 
> NOT copy when you slice them.  They return views into the slice.  This 
> break from Python semantics was done early on as a performance 
> enhancement, and can cause no end of confusion (unfortunately).


Silly me.  To add more confusion, this is a way to copy Numeric
arrays:

a[:] = b

Or you can just use .copy()


> You copy Numeric arrays with the .copy() method.  I think the 
> 'numarray' people (who are building a replacement for Numeric) have 
> gone back and forth over whether to keep the 
> slices-are-views-not-copies feature, and have decided (for 
> compatibility w/ Numeric) to keep it as is.
> 
> But regardless, since it may matter, Numeric slices do not produce 
> copies, but views (or aliases).
> 

-- 
CARL BANKS





More information about the Python-list mailing list