1

Possible Duplicate:
Python builtin “all” with generators

I've come across the following reproducible behaviour in IPython:

def test(lst):
    for val in (v is not None for v in lst):
        print val
    print all(v is not None for v in lst)
    print
    print [v is not None for v in lst]
    print all([v is not None for v in lst])
    print
    print
    for val in (v is None for v in lst):
        print val
    print all(v is None for v in lst)
    print
    print [v is None for v in lst]
    print all([v is None for v in lst])

>>> test([1])
True
True

[True]
True

False
True

[False]
False

The error originally appeared in the midst of a much larger algorithm, but it reduces to the above test case.

Now this output is clearly wrong, but I've got no idea what's going on. If I repeat in the standard python shell, it gives the expected output.

Additional Notes

The third set of print statements show the error: all(v is None for v in [1]) should return True, not False.

IPython is running with a lot of objects loaded (about 1.5GB worth, most below one parent object). If I close everything and restart, the error disappears, but it reappears if reload my data.

My thought is that there must be some memory corruption going on, but I'm stumped as to what it could be. Any ideas?

Community
  • 1
  • 1
sapi
  • 9,944
  • 8
  • 41
  • 71
  • Here's what I believe he's seeing: `all(v is None for v in lst) != all([v is None for v in lst])`. – Bill Lynch Jan 03 '13 at 04:01
  • 1
    In the third case, if you iterate through the generator, the only item is False. However, all(generator) is True – sapi Jan 03 '13 at 04:01
  • @sapi Ah, I see - I was looking at my results and not yours, apologies :) – RocketDonkey Jan 03 '13 at 04:02
  • 1
    Are you sure you're using the system `all`? Or has it been overwritten by anything? Perhaps by numpy? – Bill Lynch Jan 03 '13 at 04:03
  • 1
    `numpy.all` do not iterate on generators... try using `__builtin__.all` – JBernardo Jan 03 '13 at 04:04
  • Aha - you guys have it, thanks a heap :) I didn't realise that IPython was pulling in the numpy all. It didn't occur to me that it would replace builtins! (I've always thought it a weird choice that they've cluttered the global namespace with so much junk, but I didn't realise they'd overwrite things) – sapi Jan 03 '13 at 04:07
  • they don't pollute the global namespace for you, you pollute your global namespace by `from numpy import *`. don't do that. – SingleNegationElimination Jan 03 '13 at 04:35
  • @TokenMacGuy - I never do that, which is why this caught me by surprise. It must be an IPython default for the qtconsole (with inlined pylab). – sapi Jan 03 '13 at 04:43
  • ah.. yes, it looks like `pylab` imports "core parts of numpy, scypy and matplotlib" (http://www.scipy.org/PyLab) – SingleNegationElimination Jan 03 '13 at 05:18

0 Answers0