[Numpy-discussion] Behavior of array scalars

Alexander Belopolsky alexander.belopolsky at gmail.com
Fri Feb 17 12:51:03 EST 2006


On 2/17/06, Christopher Barker <Chris.Barker at noaa.gov> wrote:
>  >>> x += 5
>
> I expect this to change the object in place.
>
>  >>> x
> 10
>
> but what is this? is it no longer an array?

I would say it is a bug, but here is an easy work-around

>>> x = array(5)
>>> id(x)
6425088
>>> x[()]+=5
>>> id(x)
6425088
>>> x
array(10)

You can also use

>>> x[...]+=5
>>> x
array(15)

With an additional benefit that the same syntax works for any shape.

> Is there a way to set the value in place, without resorting to:


>>> x[...] = 10
or
>>> x[()] = 10


You can see more on this feature at
http://projects.scipy.org/scipy/numpy/wiki/ZeroRankArray




More information about the NumPy-Discussion mailing list