|
From: Perry G. <pe...@st...> - 2005-07-11 19:22:48
|
On Jul 11, 2005, at 2:38 PM, Curzio Basso wrote: >> An array used as an index produces a new copy (if you think about it, >> given the possibilities, there isn't much else that can be done). So >> the entity on the left of your expression is modified, but since >> nothing refers to it, it is discarded immediately thereafter. The >> array >> 'a' itself was never changed. I'd say that it isn't either a bug or >> feature. > > ok. on the other hand: > >>>> a[[0,2],0] = 1 >>>> print a > array([[ 1, 1, 2, 3], > [ 4, 5, 6, 7], > [ 1, 9, 10, 11]]) > > I have some problem understanding the difference at the implementation > level between the two cases. I would have assumed that at end they > would operate on a similar way... > > curzio I was sloppy in my description. 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. Perry |