[Numpy-discussion] Behavior of array scalars

Travis Oliphant oliphant at ee.byu.edu
Fri Feb 17 15:04:01 EST 2006

Christopher Barker wrote:

> Hi all,
> It just dawned on my that the numpy array scalars might give something 
> I have wanted once in a while: mutable scalars. However, it seems that 
> we almost, but no quite, have them. A few questions:

NumPy (starting with Numeric) has always had this love-hate relationship 
with zero-dimensional arrays.  We use them internally to simplify the 
code, but try not to expose them to the user.  Ultimately, we couldn't 
figure out how to do that cleanly and so we have the current compromise 
situation where 0-d arrays are available but treated as second-class 
citizens.   Thus, we still get funny behavior in certain circumstances.  
I think you found another such quirky area. 

I'm open to suggestions.

To analyze this particular case...

The a+= 10

operation should be equivalent to add(a,10,a). 

Note that explicitly writing


returns a scalar (all ufuncs return scalars if 0-d arrays are the 
result).   But, a is modified in-place as you wanted.

Perhaps what is going on is that

a += 10

is begin translated to

a = a + 10

rather than


I'll have to look deeper to see why.


More information about the NumPy-Discussion mailing list