[Numpy-discussion] Response to PEP suggestions
oliphant at ee.byu.edu
Fri Feb 18 14:46:10 CST 2005
>> When Python needs a scalar it will generally ask the object if it can
>> turn itself into an int or a float. A notable exception is indexing
>> in a list (where Python needs an integer and won't ask the object to
>> convert if it can). But int(b) always returns a Python integer if
>> the array has only 1 element.
> Still, this is a major point in practice. There was a Numeric release
> at some point in history that always returned rank-0 array objects (I
> don't remember if by design or by mistake), and it broke lots of my
> code because I was using array elements as indices.
I posted a question to python-dev about changing the internals of Python
to support asking objects
to convert to ints in slicing.
While open to the idea for Python 3000, Guido does not seem favorable to
the idea for Python 2.X The problem, Guido mentions was that float-like
objects can convert to ints by truncation and he doesn't want to allow
floats to be used as indexes. He feels it would break too much code.
Using this line of reasoning, then, arrays should not be used as indexes
unless they are explicitly converted to integers: int(a)
I have proposed a second solution that asks if a special check could be
made for rank-0 arrayobjects (of integer type) if and when they are
allowed in the core.
I think Konrad's valid point regarding consistency is that to the user
it looks like he is making an array of integers
a = array([1,2,3,4])
so it is confusing (the first time) if
a fails to act like an integer when requested for slicing an array.
Of course underneath, a is not an array of integers (it is an array of
homogeneous c-ints converted from the Python integer and so why should
a be a Python integer).
This is the problem. We want different things to act the same all the
time when fundamentally they are different. Python allows this in many
cases, but doesn't seem to be fully transparent in this regard.
When I first started with Python, as a MATLAB user I was confused by the
fact that lists, tuples, and arrays were all different things that had
some commonality. I was much happier when I just decided to let them be
different and write my code accordingly. (Interestingly enough since
that time MATLAB has added other types to their language as well ---
which don't always get along).
Here I think we should just let rank-0 arrays and Python scalars be
different things and let people know that instead of trying to mask the
situation which ultimately confuses things.
More information about the Numpy-discussion