-3

If I try

>>> from pylab import *
>>> b = [2, 3, 4, 5, 6, 7]
>>> a = any(x < 0 for x in b)  
>>> print(a)

it doesn't return True or False.

It returns

<generator object <genexpr> at 0x7fbd62129ab0>
Praveen
  • 6,872
  • 3
  • 43
  • 62
RUI YU
  • 51
  • 1
  • 1
    Please read [how to ask](http://stackoverflow.com/help/how-to-ask) – That1Guy Sep 06 '16 at 19:02
  • 1
    Is this all of your code? As-is, it does produce this output. – Blender Sep 06 '16 at 19:03
  • 5
    Two hundred quatloos says that you're working with `numpy.any`, not Python's `any`. `numpy.any` doesn't play well with generators. – DSM Sep 06 '16 at 19:06
  • 1
    On python 3.5 and 2.7 you code prints `False`. But if I use `numpy.any` it gives your output. Use `a = __builtins__.any(x<0 for x in b)`. This is what happens when you `import *`. – cdarke Sep 06 '16 at 19:07
  • @DSM and, once again, clear example why wildcard imports sucks and namespaces actually matters (a lot). – Łukasz Rogalski Sep 06 '16 at 19:10
  • 1
    I've edited the image into the question. Please copy-paste the terminal output directly, rather than giving a link to an image in future. – Praveen Sep 06 '16 at 19:12
  • Sometimes, it's not easy to tell that a `from numpy import *` has happened. I'm a long-time python user and rely heavily on numpy, and I just made this mistake myself, having started python as `ipython --pylab`. So I can understand how this might have happened... – Praveen Sep 06 '16 at 19:17
  • 1
    Yes. You're right. That's because of pylab. – RUI YU Sep 06 '16 at 19:39
  • 1
    With the `numpy.any` diagnosis this does not need to be put on hold. – hpaulj Sep 06 '16 at 20:06
  • @hpaulj The *question* still hasn’t improved at all. Even if we know now from the comment thread (which is also difficult to follow as of right now). – poke Sep 06 '16 at 20:53
  • 1
    All it is missing is the `from pylab import *`. – hpaulj Sep 06 '16 at 21:00

2 Answers2

4

You are using a numpy.any() instead of the built-in any(). Most probably you have from numpy import any or from numpy import *, which causes this behavior.

Why that happens?

According to the documentation, any tests if any element evaluates the condition. However, if you look into the source code, it actually returns a asanarray() result which is a generator.

How to avoid it?

It is always a good idea to import only scope rather than the method itself, like so: import numpy as np

:)

UPDATE 1

Personally, I have never used iPython, but thanks to comments by @Praveen and @hpaulj, if you use --pylab flag with ipython, you will see the same behavior, and you can turn that behavior off - never knew it! :)))

RafazZ
  • 4,049
  • 2
  • 20
  • 39
  • 1
    And this is a good example why the [Style Guide for Python Code](https://www.python.org/dev/peps/pep-0008/#imports) says: "Wildcard imports (`from import *`) should be avoided, as they make it unclear which names are present in the namespace, confusing both readers and many automated tools." – Matthias Sep 06 '16 at 19:17
  • Yep - saw your comment after editing the answer :))) – RafazZ Sep 06 '16 at 19:21
  • 1
    You could also run into this issue if you started python as `ipython --pylab`, as many scientific programming tutorials will instruct you to. – Praveen Sep 06 '16 at 19:22
  • @Praveen - I didn't know that, just checked :) I guess that all the modules are imported automatically. I wonder if it might cause any problems/conflicts with the built-in functions. – RafazZ Sep 06 '16 at 19:28
  • 1
    It is possible to turn off the `*` imports with `--pylab` - but it requires a flag in the setup (or option in the command line). – hpaulj Sep 06 '16 at 20:08
2

it returns false

 >> b = [2,3,4,5,6,7]
 >>> b
 [2, 3, 4, 5, 6, 7]
 >>> a = any(x<0 for x in b)
 >>> a
 False
 >>> print(a)
 False
tessie
  • 964
  • 3
  • 14
  • 24