[Numpy-discussion] Bug in numpy all() function
Wed Feb 6 14:03:20 CST 2008
Dan Goodman wrote:
> Hi all,
> I think this is a bug (I'm running Numpy 188.8.131.52):
>>>> from numpy import *
>>>> def f(x): return False
>>>> all(f(x) for x in range(10))
> I guess the all function doesn't know about generators?
Yup. It works on arrays and things it can turn into arrays by calling the C API
equivalent of numpy.asarray(). There's a ton of magic and special cases in
asarray() in order to interpret nested Python sequences as arrays. That magic
works fairly well when we have sequences with known lengths; it fails utterly
when given an arbitrary iterator of unknown length. So we punt. Unfortunately,
what happens then is that asarray() sees an object that it can't interpret as a
sequence to turn into a real array, so it makes a rank-0 array with the iterator
object as the value. This evaluates to True.
It's possible that asarray() should raise an exception for generators, but it
would be a special case. We wouldn't be able to test for arbitrary iterables.
"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco
More information about the Numpy-discussion