[NumPy-Tickets] [NumPy] #1651: Inconsistency with __index__() for rank-1 arrays?
NumPy Trac
numpy-tickets@scipy....
Thu Oct 28 10:13:58 CDT 2010
#1651: Inconsistency with __index__() for rank-1 arrays?
--------------------+-------------------------------------------------------
Reporter: faltet | Owner: somebody
Type: defect | Status: new
Priority: normal | Milestone: 2.0.0
Component: Other | Version: 1.5.0
Keywords: |
--------------------+-------------------------------------------------------
I find this a bit misleading:
{{{
>>> a = np.arange(10)
>>> a[np.array(0)]
0
>>> a[np.array([0])]
array([0])
>>> a[[0]]
array([0])
}}}
But, for regular python lists we have:
{{{
>>> l = a.tolist()
>>> l[np.array(0)]
0
>>> l[np.array([0])]
0
}}}
i.e. indexing with a rank-0 array and a rank-1 array with one single
element return the same result, which I find inconsistent with the
expected behaviour for this case, i.e.:
{{{
>>> l[[0]]
---------------------------------------------------------------------------
TypeError Traceback (most recent call
last)
/tmp/tables-2.2/<ipython console> in <module>()
TypeError: list indices must be integers, not list
}}}
The ultimate reason for all of this is:
{{{
>>> np.array(0).__index__()
0
>>> np.array([0]).__index__()
0
}}}
But I wonder why !NumPy needs the latter behaviour, instead of the more
logical:
{{{
>>> np.array([0]).__index__()
---------------------------------------------------------------------------
TypeError Traceback (most recent call
last)
/tmp/tables-2.2/<ipython console> in <module>()
TypeError: only rank-0 integer arrays can be converted to an index
}}}
This inconsistency has indeed introduced a bug in my application and for
solving this I'd need something like:
{{{
def is_idx(index):
"""Check if an object can work as an index or not."""
if hasattr(index, "__index__"): # Only works on Python 2.5 on
if (hasattr(index, "shape") and index.shape == (1,)):
return False
try:
idx = index.__index__()
return True
except TypeError:
return False
return False
}}}
i.e. for determining if an object can be an index or not, I need to
explicitly check for a shape different from (1,), which is unnecessarily
complicated.
So I find the current behaviour prone to introduce errors in apps and I'm
wondering why exactly {{{np.array([1])}}} should work as an index at all.
It would not be better if {{{np.array([1]).__index__()}}} would raise a
`TypeError`?
Thanks
--
Ticket URL: <http://projects.scipy.org/numpy/ticket/1651>
NumPy <http://projects.scipy.org/numpy>
My example project
More information about the NumPy-Tickets
mailing list