[Numpy-discussion] Matching 0-d arrays and NumPy scalars

dmitrey dmitrey.kroshko@scipy....
Thu Feb 21 01:47:58 CST 2008

Travis E. Oliphant wrote:
> Hi everybody,
> In writing some generic code, I've encountered situations where it would 
> reduce code complexity to allow NumPy scalars to be "indexed" in the 
> same number of limited ways, that 0-d arrays support.
> For example, 0-d arrays can be indexed with
>     * Boolean masks
>     * Ellipses x[...]  and x[..., newaxis]
>     * Empty tuple x[()]
> I think that numpy scalars should also be indexable in these particular 
> cases as well (read-only of course,  i.e. no setting of the value would 
> be possible).
> This is an easy change to implement, and I don't think it would cause 
> any backward compatibility issues.
> Any opinions from the list?
> Best regards,
> -Travis O.
As for me I would be glad to see same behavior for numbers as for arrays 
at all, like it's implemented in MATLAB, i.e.
ok, for numpy having at least possibility to use
print a[0]
would be very convenient, now atleast_1d(a) is required very often, and 
sometimes errors occur only some times later, already during execution 
of user-installed code, when user usually pass several-variables arrays 
and some time later suddenly single-variable array have been encountered.
I guess it could be implemented via a simple check:
if user calls for a[0] and a is array of shape () (i.e. like 
a=array(80)) then return a[()]

More information about the Numpy-discussion mailing list