Rich Comparisons Gotcha
James Stroud
jstroud at mbi.ucla.edu
Mon Dec 8 01:05:54 EST 2008
Robert Kern wrote:
> James Stroud wrote:
>> py> 112 = [1, y]
>> py> y in 112
>> Traceback (most recent call last):
>> File "<stdin>", line 1, in <module>
>> ValueError: The truth value of an array with more than one element is...
>>
>> but not
>>
>> py> ll1 = [y,1]
>> py> y in ll1
>> True
>>
>> It's this discrepancy that seems like a bug, not that a ValueError is
>> raised in the former case, which is perfectly reasonable to me.
>
> Nothing to do with numpy. list.__contains__() checks for identity with
> "is" before it goes to __eq__().
...but only for the first element of the list:
py> import numpy
py> y = numpy.array([1,2,3])
py> y
array([1, 2, 3])
py> y in [1, y]
------------------------------------------------------------
Traceback (most recent call last):
File "<ipython console>", line 1, in <module>
<type 'exceptions.ValueError'>: The truth value of an array with more
than one element is ambiguous. Use a.any() or a.all()
py> y is [1, y][1]
True
I think it skips straight to __eq__ if the element is not the first in
the list. That no one acknowledges this makes me feel like a conspiracy
is afoot.
More information about the Python-list
mailing list