[Numpy-discussion] truthiness of object arrays

Antony Lee antony.lee at berkeley.edu
Thu Nov 13 12:37:02 EST 2014


On Python3, __nonzero__ is never defined (always raises an AttributeError),
even after calling __bool__.

2014-11-13 5:24 GMT-08:00 Alan G Isaac <alan.isaac at gmail.com>:

> 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
>
>
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20141113/fffb024d/attachment.html>


More information about the NumPy-Discussion mailing list