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

Ralf Juengling juenglin at cs.pdx.edu
Sat Mar 19 21:09:14 EST 2005


Travis,

Discussing zero dimensional arrays, the PEP says at one point:

   ... When ndarray is imported, it will alter the numeric table
   for python int, float, and complex to behave the same as 
   array objects. 

   Thus, in the proposed solution, 0-dim arrays would never be
   returned from calculation, but instead, the equivalent Python
   Array Scalar Type.  Internally, these ArrayScalars can
   be quickly converted to 0-dim arrays when needed.  Each scalar
   would also have a method to convert to a "standard" Python Type
   upon request (though this shouldn't be needed often).


I'm not sure I understand this. Does it mean that, after having
imported ndarray, "type(1)" to "ndarray.IntArrType" rather than 
"int"?

If so, I think this is a dangerous idea. There is one important
difference between zero dimensional arrays and Python scalar 
types, which is not discussed in the PEP: arrays are mutable, 
Python scalars are immutable.

When Guido introduced in-place operators in Python, (+=, *=, 
etc.) he decided that "i += 1" should be allowed for Python
scalars and should mean "i = i + 1". Here you have it, it 
means something different when i is a mutable zero dimensional
array. So, I suspect a tacit re-definition of Python scalars
on ndarray import will break some code out there (code, that
does not deal with arrays at all). 

Facing this important difference between arrays and Python
scalars, I'm also not sure anymore that advertising zero
dimensional arrays as essentially the same as Python scalars
is such a good idea. Perhaps it would be better not to try to
inherit from Python's number types and all that. Perhaps it
would be easier to just say that indexing an array always 
results in an array and that zero dimensional arrays can be 
converted into Python scalars. Period.

Ralf


PS: You wrote two questions about zero dimensional arrays 
vs Python scalars into the PEP. What are your plans for 
deciding these?











More information about the NumPy-Discussion mailing list