Why Does Python's `any` Function Not Return True Or False?
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. I
Solution 1:
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! :)))
Solution 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
Post a Comment for "Why Does Python's `any` Function Not Return True Or False?"