If numpy.any()
returns True
the comparison with is True
fails but with == True
works. Does anyone know why?
A minimal example
from __future__ import print_function
import numpy
a = numpy.array([True])
if a.any() == True:
print('== works')
if a.any() is True:
print('is works')
The output of this code is just == works
.
numpy
has it's own booleans, numpy.True_
and numpy.False_
that have different identities from Python's native booleans. Anyway, you should be using ==
for such equity comparisons
>>> a.any() is True
False
>>> a.any() is numpy.True_
True
>>> True is numpy.True_
False
>>> True == numpy.True_
True
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With