On Tue, Jan 31, 2012 at 13:26, Neal Becker <ndbeck...@gmail.com> wrote: > I was just bitten by this unexpected behavior: > > In [24]: all ([i> 0 for i in xrange (10)]) > Out[24]: False > > In [25]: all (i> 0 for i in xrange (10)) > Out[25]: True > > Turns out: > In [31]: all is numpy.all > Out[31]: True > > So numpy.all doesn't seem to do what I would expect when given a generator. > Bug?
Expected behavior. numpy.all(), like nearly all numpy functions, converts the input to an array using numpy.asarray(). numpy.asarray() knows nothing special about generators and other iterables that are not sequences, so it thinks it's a single scalar object. This scalar object happens to have a __nonzero__() method that returns True like most Python objects that don't override this. In order to use generic iterators that are not sequences, you need to explicitly use numpy.fromiter() to convert them to ndarrays. asarray() and array() can't do it in general because they need to autodiscover the shape and dtype all at the same time. -- Robert Kern "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 _______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion