[Numpy-discussion] Thoughts about zero dimensional arrays vs Python scalars

Robert Kern rkern at ucsd.edu
Sun Mar 20 15:29:20 EST 2005

Colin J. Williams wrote:

> The argument made in the PEP against returning Python scalars is:
>         Cons:  It is difficult to write generic code because scalars
>                do not have the same methods and attributes as arrays.
>                (such as .type  or .shape).  Also Python scalars have
>         different numeric behavior as well.
>                This results in a special-case checking that is not 
>                pleasant.  Fundamentally it lets the user believe that 
>                somehow multidimensional homoegeneous arrays
>                are something like Python lists (which except for
>                Object arrays they are not).
> I suggest that, in striking the balance between the developer or generic 
> writer and the end user,
> the greater design consideration should go to the ease and convenience 
> of the end user.

How are you defining "end user"? To my definition, an end user will 
neither know nor care whether rank-0 arrays or Python ints, longs, 
floats, or complexes are returned. They will be at a GUI seeing graphs 
or reading output. They won't see a bit of code.

The "generic code" being talked about in the PEP isn't code inside 
Numeric itself. It's all of the stuff written *using* Numeric. Now, if 
you are defining "end user" to be the people using Numeric to write 
code, then we can argue about which choice is simpler or more 
convenient. There are some situations in which the rank-0 approach is 
more convenient and some in which the Python scalar is preferred. I'm 
not sure that we can reliably enumerate them.

I would suggest that Option 2, returning Python types when the typecode 
allows and rank-0 arrays otherwise, is an inconsistency that we could do 

Robert Kern
rkern at ucsd.edu

"In the fields of hell where the grass grows high
  Are the graves of dreams allowed to die."
   -- Richard Harter

More information about the NumPy-Discussion mailing list