[Matrix-SIG] Feature of numeric slicing?

Konrad Hinsen hinsen@ibs.ibs.fr
Wed, 25 Mar 1998 18:57:50 +0100


> >>>a = Numeric.arrayrange(0,5)
> >>>a
> >>>array[0,1,2,3,4]
> >>>c = a[1:3]
> >>>c
> >>>array[1,2]
> >>>c[0] = 99
> >>>a
> >>>array[0, 99, 2, 3, 4]
> >>>c
> >>>array[99,2]
> Is this a desired property, a feature or a bug? I have not seen

Feature.

> It appears that c just points to a section of the array a.
> If an operation such as c = a[1:3]*2.0 is carried out then
> c is a new object but a plain slice retains a connection of
> slicer and slicee.

Exactly.

> In Python it is my understanding that a slice creates a new object, without
> references to the slicee.

True for lists, but not for NumPy arrays. It is simply very useful
to be able to share the array data space in this way. On the other
hand, it can lead to bad surprises, so it should probably be pointed
out somewhere clearly.
-- 
-------------------------------------------------------------------------------
Konrad Hinsen                          | E-Mail: hinsen@ibs.ibs.fr
Laboratoire de Dynamique Moleculaire   | Tel.: +33-4.76.88.99.28
Institut de Biologie Structurale       | Fax:  +33-4.76.88.54.94
41, av. des Martyrs                    | Deutsch/Esperanto/English/
38027 Grenoble Cedex 1, France         | Nederlands/Francais
-------------------------------------------------------------------------------