[SciPy-Dev] docstring standard: parameter shape description

Nathaniel Smith njs@pobox....
Tue Jan 29 18:21:48 CST 2013


On Tue, Jan 29, 2013 at 3:06 PM, Joe Harrington <jh@physics.ucf.edu> wrote:
> I did the following test before I posted:
>
>>>> import numpy as np
>>>> a=np.array(((2,3),(3,4)))
>>>> a
> array([[2, 3],
>        [3, 4]])
>>>> a.shape
> (2, 2)
>>>> a.shape=(2,0,2)
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
> ValueError: total size of new array must be unchanged
>
> So, you can't have a 0-element dimension in an array that is not already
> 0-size.

Yes, but you can't create a 7-element dimension in an array that is
already not a multiple-of-7 in size, without this ruling out the idea
of 7-element dimensions as a thing :-).

> I didn't think about 0-size arrays having some non-zero
> dimensions.  I guess that makes mathematical sense, but then what is the
> point?
>
> Between this and your bug report, I wonder whether a 0-element array
> dimension is a feature you can rely on.  Can a developer let us know if
> it's an accident that it works at all?  I'll leave it to you to start a
> new thread on this and get the inconsistencies resolved, since you're
> the one using the feature.  The report was last touched 3 years ago...

It's certainly intended, and very useful! Most code that can handle an
arbitrary size for a dimension can automatically handle a 0 size, and
often this lets you avoid having to write in special cases. This is
also why e.g. people have spent a lot of effort thinking about what it
means to sum 0 elements:
  https://en.wikipedia.org/wiki/Empty_sum
(And numpy does the Right Thing for expressions like np.sum([]),
np.prod([]).) I don't know about that particular bug report's current
status, but just because there's some obscure corner of the fancy
indexing system that has a bug it doesn't mean that we don't support 0
sized arrays in general... that would mean a lot of numpy was
unsupported! ;-)

-n


More information about the SciPy-Dev mailing list