[Python-Dev] PyObject_RichCompareBool identity shortcut

Greg Ewing greg.ewing at canterbury.ac.nz
Fri Apr 29 03:08:05 CEST 2011


Nick Coghlan wrote:

> I hadn't really thought about it that way before this discussion - it
> is the identity checking behaviour of the builtin containers that lets
> us sensibly handle cases like sets of NumPy arrays.

Except that it doesn't:

 >>> from numpy import array
 >>> a1 = array([1,2])
 >>> a2 = array([3,4])
 >>> s = set([a1, a2])
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'numpy.ndarray'

Lists aren't trouble-free either:

 >>> lst = [a1, a2]
 >>> a2 in lst
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
ValueError: The truth value of an array with more than one element is ambiguous. 
Use a.any() or a.all()

-- 
Greg


More information about the Python-Dev mailing list