[Numpy-discussion] rank-0 arrays

Perry Greenfield perry at stsci.edu
Fri Sep 13 07:06:14 CDT 2002


Konrad Hinsen writes:
>
> Pearu Peterson <pearu at cens.ioc.ee> writes:
>
> > I think it would be confusing if the result of repr would be `2' and not
> > `array(2)' because 2 and array(2) are not equivalent in all usages
> > but it should be clear from repr results as a first way to learn more
> > about the objects.
>
> I agree. There will already be some inevitable confusion with both
> rank-0 arrays and scalars around, with similar but not identical
> behaviour. Rank-0 arrays shouldn't make it worse by using camouflage.
>
I also agree that having repr hide the fact that it is an array is
a bad thing. But do you find getting "array(2)" when indexing
a single item acceptable when working interactively? I agree that
displayhook is probably the best way of altering this behavior if
a user desires (perhaps we will provide a module function to do so, but
not enable it by default).

> >
> > Could array.__getitem_ and __getslice__ detect if their argument is
> > an array and skip using Python objects when iterating over indices?
>
> Of course they know that they are indexing an array, they are
> defined at the level of the array class/type. However, they cannot
> detect an iteration as opposed to a single item access.
>
I don't know that it is desirable either to have iteration
return different kinds of objects than explicit indexing, even
if possible. Do we really want

for val in x:
    ...

to use scalars while

for i in range(len(x)):
    val = x[i]
    ...

uses rank-0 arrays? I'm inclined not to have different behavior for
what seems like identical iteration.

> I don't know if this efficiency problem could be important in
> practice, probably only practice can tell. I have no idea how many
> single-item indexing operations into arrays occur in my code, this is
> not something I worried about when writing it.
>
> If there will be scalar and rank-0 array returning variants of
> indexing anyway, then I suppose that changing the index syntax
> to one or the other is not a big effort. So my suggestion is to
> make a test release and see what the reactions are.
>
That is our plan (hence the reference to "experimental"), but I
wanted to see if there were strong feelings on this before doing so.

Perry





More information about the Numpy-discussion mailing list