[NumPy-Tickets] [NumPy] #1433: numpy array captures 'in' statement when it shouldn't
NumPy Trac
numpy-tickets@scipy....
Tue Mar 16 17:27:46 CDT 2010
#1433: numpy array captures 'in' statement when it shouldn't
--------------------------+-------------------------------------------------
Reporter: graik | Owner: somebody
Type: defect | Status: new
Priority: normal | Milestone:
Component: numpy.core | Version: 1.3.0
Keywords: __contains__ |
--------------------------+-------------------------------------------------
The following code used to work perfectly well with older numpy versions:
>>> a = arange(10)
>>> a in (0,None)
FALSE
Now it gives an error:
ValueError: The truth value of an array with more than one element is
ambiguous. Use a.any() or a.all()
This comes unexpected. For example my library contains several situations
like this:
def calculate( a=None ):
if a in (0,None):
a = zeros( 10 )
...
calculate( a=arange(10) )
So I use the 'in' to quickly check for identity with a couple of non-valid
values.
Apparently, the numpy array code now captures the 'in' statement via
__contains__ and expects an array on the other side (not even a list will
do). This is not logical. While there *might* be some reason for treating
the 'array in array' situation differently, this should not break standard
python behavior.
Simple python lists do accept this code::
>>> l = [1,2,3]
>>> l in (None,0)
FALSE
I would expect numpy arrays to do the same (as it did before). At the very
least, the new __contains__ should check for non-array arguments and fall
back to the standard behavior rather than raising an exception.
Thanks in advance!
Greetings
Raik
--
Ticket URL: <http://projects.scipy.org/numpy/ticket/1433>
NumPy <http://projects.scipy.org/numpy>
My example project
More information about the NumPy-Tickets
mailing list