Explanation of list reference

Steven D'Aprano steve+comp.lang.python at pearwood.info
Mon Feb 17 00:57:13 EST 2014


On Mon, 17 Feb 2014 11:40:57 +1300, Gregory Ewing wrote:

> Steven D'Aprano wrote:
>> And indeed numpy arrays do share state. Why? No idea. Somebody thought
>> that it was a good idea. (Not me though...)
> 
> Probably because they're often large and people don't want to incur the
> overhead of copying them any more than necessary. So slices are defined
> to return views rather than independent objects.

I don't have a problem with slices returning views. But they should claim 
to be views, not claim to be arrays:


py> from numpy import array
py> a = array([1, 2, 3, 4])
py> b = a[:]
py> type(a) is type(b)
True
py> b[1] = 99
py> a
array([ 1, 99,  3,  4])


You can do this to distinguish the two cases:

py> a.base
py> b.base is a
True

but I think a dedicated array_view type would have be better.



-- 
Steven



More information about the Python-list mailing list