[Numpy-discussion] bug or feature?
jmiller at stsci.edu
Thu Jul 14 02:44:59 CDT 2005
On Mon, 2005-07-11 at 15:22 -0400, Perry Greenfield wrote:
> For array indexing, assignment does
> modify the original array (as your case above illustrates), but once
> you sliced the array-indexed array, it changed the context of the
> indexing. I.e., (to take a simpler, 1-d array x)
> x[[0,2]] = 1 # modifies x
> This form results in __setitem__ being called on x
> x[[0,2]][:4] = 1 # doesn't modify x
> This form results in __getitem__ being called on x (and thus producing
> a new copy of the array) for the array indexing and __setitem__ being
> called for the slice. At least that's what I think is happening. Todd
> or someone more recently familiar with how Python handles this can
> correct me if I'm wrong.
This is indeed what happens. A temporary is created by the first sub-
expression x[[0,2]], modified by the slice subexpression [:4] = 1, and
then discarded. I think the array indexing "grammar" could be changed
to support x[[0,2],:4] as a single expression which would create the
temporary and then copy-back at the end... but numarray's array
indexing is not currently that fancy.
More information about the Numpy-discussion