[Numpy-discussion] truthiness of object arrays

Alan G Isaac alan.isaac at gmail.com
Thu Nov 13 08:24:37 EST 2014


On 11/13/2014 1:19 AM, Antony Lee wrote:
> "t.__bool__()" also returns True


But t.__nonzero__() is being called in the `if` test.
The question is: is the difference between `__nonzero__`
and `__bool__` intentional.

By the way, there has been a change in behavior.
For example, in 1.7.1 if you call `t.__bool__()`
it raised an attribute error -- unless one first
called `t.__nonzero__()` and then called `t.__bool__()`,
which was of course very weird and needed to be fixed.
Maybe (?) not like this.

In fact the oddity probably remains but moved. in 1.9.0 I see this:

 >>> np.__version__
'1.9.0'
 >>> t = np.array(None); t[()] = np.array([None, None])
 >>> t.__nonzero__()
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
AttributeError: 'numpy.ndarray' object has no attribute '__nonzero__'
 >>> t.__bool__()
True
 >>> t.__nonzero__()
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

Alan Isaac





More information about the NumPy-Discussion mailing list