The reason it raises the value error is that with a recent enough version of Numpy the __eq__
override of arrays does an elementwise object comparison even when comparing to None.
With Numpy 1.12.1:
In [2]: if np.array([1,2,3]) == None:
...: print('Equals None')
...: else:
...: print('Does not equal None')
...:
/home/user/Work/SO/bin/ipython:1: FutureWarning: comparison to `None` will result in an elementwise object comparison in the future.
#!/home/user/Work/SO/bin/python3
Does not equal None
and with Numpy 1.13.1:
In [1]: if np.array([1,2,3]) == None:
...: print('Equals None')
...: else:
...: print('Does not equal None')
...:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-1-06133f6732e1> in <module>()
----> 1 if np.array([1,2,3]) == None:
2 print('Equals None')
3 else:
4 print('Does not equal None')
5
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
The error is quite self explanatory. Numpy has a different view on truthiness of an array compared to how plain Python sequences behave.
In order to check if an object is the singleton value None, you should use identity comparison as also explained here:
def check(x):
if x is None:
print('x is None')
else:
print('x is not None')