surprising result all (generator) (bug??)
mdickinson at enthought.com
Tue Jan 31 09:25:33 EST 2012
On Jan 31, 7:24 am, Neal Becker <ndbeck... at gmail.com> wrote:
> In : all is numpy.all
> Out: True
> Excellent detective work, Mark! But it still is unexpected, at least to me.
Agreed that it's a bit surprising. It's a consequence of NumPy's
general mechanisms for converting arbitrary inputs to arrays:
>>> from numpy import asarray
>>> asarray([i > 0 for i in range(10)])
array([False, True, True, True, True, True, True, True, True,
>>> asarray(i > 0 for i in range(10))
array(<generator object <genexpr> at 0x4688aa8>, dtype=object)
So in the second case you get a 0-dimensional array of type 'object',
whose only element is that generator object; not surprisingly, the
generator object is considered true.
As to why it's this way: best to ask on the NumPy mailing lists.
It's probably something that's quite difficult to change without
breaking lots of code, though.
More information about the Python-list