[Numpy-discussion] Scalar array - a trap for the unwary

Colin J. Williams cjw at sympatico.ca
Sat Jan 21 16:12:02 EST 2006


In the script below, b is presented as an integer, either with str or 
repr, but it is an array and needs further processing to treat it as in 
integer.

I much prefer the numarray treatment, particularly in a matrix context.  
I suggest that, if it looks like an integer, b[1] should return a Python 
scalar.

Script:
# tScalar.py   Scalar vs rank 0
import numpy.core.multiarray as _mu
a= _mu.array([1, 2, 4])
print 'a:', a, 'repr(a):', repr(a), 'shape:', a.shape, 'dtype:', a.dtype
b= a[1]
print 'b:', b, 'repr(b):', repr(b), 'shape:', b.shape, 'dtype:', b.dtype

Result:
a: array([1, 2, 4], 'l') repr(a): array([1, 2, 4], 'l') shape: (3,) 
dtype: <type 'int32_arrtype'>
b: 2 repr(b): 2 shape: () dtype: <type 'int32_arrtype'>

Colin W.





More information about the NumPy-Discussion mailing list