[Python-Dev] Pre-PEP: Allow Empty Subscript List Without Parentheses
Tim Hochberg
tim.hochberg at ieee.org
Sat Jun 10 05:41:00 CEST 2006
Alex Martelli wrote:
> On Jun 9, 2006, at 4:55 PM, Greg Ewing wrote:
> ...
>
>>Think about how you get from an N dimensional array to
>>an N-1 dimensional array: you index it, e.g.
>>
>> A2 = [[1, 2], [3, 4]] # a 2D array
>>
>> A1 = A2[1] # a 1D array
>>
>> A0 = A1[1] # a 0D array???
>>
>> print A0
>>
>>What do you think this will print?
>
>
> Don't confuse arrays with lists...:
>
> >>> A2 = Numeric.array([[1, 2], [3, 4]], Numeric.Float32)
> >>> A1 = A2[1]
> >>> A0 = A1[1]
> >>> type(A0)
> <type 'array'>
> >>>
>
> It doesn't work the same if you specify Numeric.Float64 instead -- an
> ancient wart of Numeric, of course. Still, Numeric and its
> descendants are "the" way in Python to get multi-dimensional arrays,
> since the stdlib's array module only supports one-dimensional ones,
> and lists are not arrays.
Note that this wart has been pretty much killed in numpy by supplying a
full complement of scalar types:
>>> import numpy
>>> A2 = numpy.array([[1,2], [3,4]], numpy.float32)
>>> A1 = A2[1]
>>> A0 = A1[1]
>>> A0
4.0
>>> type(A0)
<type 'float32scalar'>
The same excercise with float64 will give you a float64 scalar. The
behaviour in this area is overall much more consistent now. You can
still get a 0-D array by doing array(4.0) and possibly a few other ways,
but there much less common. These scalar objects are immutable, but have
all (or at least most) of the the array methods and attributes. For example:
>>> A0.dtype
dtype('<f4')
dtype is more or less equivalent to Numeric's typecode().
-tim
More information about the Python-Dev
mailing list