Ah, so all this trouble is to circumvent the special case in indexing where
true scalar indices and 0d array indices both return scalars? Seems like we
could use a more systematic way of handling these things in general but now
it does make sense. If you can think of a better comment that might be nice
(maybe something like "size=None and size=() both indicate the caller wants
a scalar, but for the former they want it as a regular python object, and
for the latter they want it as a 0d array. a[idx] will return a python
object regardless, so for the size=() case we need to put it back into an
array. Bu we can't just call asarray, because the autodetection logic might
mangle object or void arrays.")

Anyway now I understand it all and am happy for the code to go in.
On 2 Mar 2013 23:54, "seberg" <notifications@github.com> wrote:

> In numpy/random/mtrand/mtrand.pyx:
>
> >              return idx
> > -        res = a[idx]
> > -        # Note when introducing an axis argument a copy should be ensured.
> > -        if res.ndim == 0 and shape is not None:
> > -            # the result here is not a scalar but an array.
> > -            return np.array(res)
> > -        return res
> > +
> > +        if shape is not None and idx.ndim == 0:
> > +            # the result here is not a scalar but an array. Create the new
> > +            # array first to avoid an object array containing an array from
> > +            # misbehaving
> > +            res = np.empty((), dtype=a.dtype)
> > +            res[()] = a[idx]
> > +            return res
>
> Before I try to explain it in some non-understandable way, here an example:
>
> In [3]: a = np.array([(1,2), 3], dtype=object)
>
> In [4]: a[0] # not an array, just the tuple, but size=() should be a 0-d array to be quite clean.
> Out[4]: (1, 2)
>
> In [5]: np.array(a[0]) # not an array with a tuple inside.
> Out[5]: array([1, 2])
>
> In [6]: res = np.empty((), dtype=a.dtype)
>
> In [7]: res[()] = a[0]
>
> In [8]: res # OK now
> Out[8]: array((1, 2), dtype=object)
>
> So yeah, that whole dance is just to allow size=() to actually return 0-d
> arrays...
>
> —
> Reply to this email directly or view it on GitHub<https://github.com/numpy/numpy/pull/2992/files#r3216438>
> .
>