Re: Rich Comparisons Gotcha
- From: James Stroud <jstroud@xxxxxxxxxxxx>
- Date: Sun, 07 Dec 2008 22:05:54 -0800
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.
.
- Follow-Ups:
- Re: Rich Comparisons Gotcha
- From: Robert Kern
- Re: Rich Comparisons Gotcha
- References:
- Re: Rich Comparisons Gotcha
- From: Rasmus Fogh
- Re: Rich Comparisons Gotcha
- From: James Stroud
- Re: Rich Comparisons Gotcha
- From: Steven D'Aprano
- Re: Rich Comparisons Gotcha
- From: James Stroud
- Re: Rich Comparisons Gotcha
- From: Robert Kern
- Re: Rich Comparisons Gotcha
- From: James Stroud
- Re: Rich Comparisons Gotcha
- From: Robert Kern
- Re: Rich Comparisons Gotcha
- Prev by Date: Re: python book for non technical absolute beginner
- Next by Date: Re: A question about reference in Python.
- Previous by thread: Re: Rich Comparisons Gotcha
- Next by thread: Re: Rich Comparisons Gotcha
- Index(es):
Relevant Pages
|