surprising result all (generator) (bug??)

N

Neal Becker

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
 
M

Mark Dickinson

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

What does:

give you?
 
N

Neal Becker

Mark said:
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

What does:

give you?
In [31]: all is numpy.all
Out[31]: True

Excellent detective work, Mark! But it still is unexpected, at least to me.
 
M

Mark Dickinson

In [31]: all is numpy.all
Out[31]: 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,
True], dtype=bool)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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,150
Messages
2,570,853
Members
47,393
Latest member
silloma

Latest Threads

Top