[Numpy-discussion] truthiness of object arrays

Sebastian Berg sebastian at sipsolutions.net
Thu Nov 13 08:53:09 EST 2014


On Do, 2014-11-13 at 08:24 -0500, Alan G Isaac wrote:
> 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()
> 

Note that the error is set, but appears to be not indicated within the
C-api. This can cause errors to be missed or appear later then they
should... Might be just that you are seeing, though I could not find a
reason for it in the numpy code on first glance.

- Sebastian

> Alan Isaac
> 
> 
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
> 

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 819 bytes
Desc: This is a digitally signed message part
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20141113/a465d40a/attachment.sig>


More information about the NumPy-Discussion mailing list