[Numpy-discussion] strange behavior with a numpy array as member of a list
Tim Hirzel
hirzel at resonon.com
Thu Nov 30 15:44:11 CST 2006
Excellent. That explains it.
thank you Robert,
tim
Robert Kern wrote:
> Tim Hirzel wrote:
>
>> Thanks Robert.
>> That makes more sense now. Although I am still a little puzzled by the
>> equality behavior of an array. for example:
>>
>> >>> a = numpy.arange(5)
>> >>> None == a
>> False
>>
>> >>> 5 == a
>> array([False, False, False, False, False], dtype=bool)
>>
>> >>> class Empty:
>> pass
>> >>> e = Empty()
>> >>> e == a
>> array([False, False, False, False, False], dtype=bool)
>>
>> >>> L = []
>> >>> L == a
>> False
>>
>> #this behavior seems consistent regardless of the dtype of the array:
>> >>> b = numpy.array([None, 5])
>> >>> b
>> array([None, 5], dtype=object)
>> >>> b == 4
>> array([False, False], dtype=bool)
>>
>> What determines if an array tests for equality as the entire array or
>> element-wise?
>>
>
> There are some special cases for backwards compatibility. They are covered in
> this portion of arrayobject.c in the function array_richcompare():
>
> case Py_EQ:
> if (other == Py_None) {
> Py_INCREF(Py_False);
> return Py_False;
> }
> /* Try to convert other to an array */
> if (!PyArray_Check(other)) {
> typenum = self->descr->type_num;
> if (typenum != PyArray_OBJECT) {
> typenum = PyArray_NOTYPE;
> }
> array_other = PyArray_FromObject(other,
> typenum, 0, 0);
> /* If not successful, then return False
> This fixes code that used to
> allow equality comparisons between arrays
> and other objects which would give a result
> of False
> */
> if ((array_other == NULL) || \
> (array_other == Py_None)) {
> Py_XDECREF(array_other);
> PyErr_Clear();
> Py_INCREF(Py_False);
> return Py_False;
> }
> }
>
>
More information about the Numpy-discussion
mailing list