[Numpy-discussion] weird numpy.any() behavior; bug, or 6-legged feature?
Matthew Koichi Grimes
mkg at cs.nyu.edu
Tue Jan 16 09:06:58 CST 2007
Numpy's any() function gives unintuitive results when given a generator
rather than a sequence:
>>> import numpy as N
>>> N.any( i < 0 for i in range(3) )
If the generator is instead given as a list (using a list
comprehension), then the expected answer is given:
>>> N.any( [i < 0 for i in range(3)] )
Python 2.5's built-in any() seems to deal fine with generators:
>>> any( i < 0 for i in range(3) )
My first guess at what's happening in numpy.any() is that the generator
object itself is being interpreted as a "True", in the type-weak C sense
of the word.
Is this a bug? It seems like having to generate a whole list before
calling any() defeats the whole appeal of using any() in the first
place; namely that it'll exit on the first False returned by the
generator, without going through the rest of the elements.
More information about the Numpy-discussion