I'm also in favor of fixing this, although we might need a deprecation cycle with a warning advising to use arr.size in future to detect emptiness - just in case anyone is using it.<br><br><div class="gmail_quote"><div dir="ltr">On Sat, Aug 19, 2017, 06:01 Stephan Hoyer <<a href="mailto:shoyer@gmail.com">shoyer@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">I agree, this behavior seems actively harmful. Let's fix it. </div><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Aug 18, 2017 at 2:45 PM, Michael Lamparski <span dir="ltr"><<a href="mailto:diagonaldevice@gmail.com" target="_blank">diagonaldevice@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div>Greetings, all.  I am troubled.</div><div><br></div><div>The TL;DR is that `bool(array([])) is False` is misleading, dangerous, and unnecessary. Let's begin with some examples:</div><div><br></div><div>>>> bool(np.array(1))<br></div><div>True</div><div>>>> bool(np.array(0))</div><div>False</div><div>>>> bool(np.array([0, 1]))<br></div><div>ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()</div><div><div>>>> bool(np.array([1]))<br></div><div>True</div></div><div><div>>>> bool(np.array([0]))<br></div><div>False</div></div><div>>>> bool(np.array([]))<br></div><div>False</div><div><br>One of these things is not like the other.</div><div><br></div><div>The first three results embody a design that is consistent with some of the most fundamental design choices in numpy, such as the choice to have comparison operators like `==` work elementwise.  And it is the only such design I can think of that is consistent in all edge cases. (see footnote 1)</div><div><br></div><div>The next two examples (involving arrays of shape (1,)) are a straightforward extension of the design to arrays that are isomorphic to scalars.  I can't say I recall ever finding a use for this feature... but it seems fairly harmless.</div><div><br></div><div>So how about that last example, with array([])?  Well... it's /kind of/ like how other python containers work, right? Falseness is emptiness (see footnote 2)...  Except that this is actually *a complete lie*, due to /all of the other examples above/!</div><div><br></div><div>Here's what I would like to see:</div><div><br></div><div>>>> bool(np.array([]))<br></div><div><div>ValueError: The truth value of a non-scalar array is ambiguous. Use a.any() or a.all()</div></div><div><br></div><div>Why do I care?  Well, I myself wasted an hour barking up the wrong tree while debugging some code when it turned out that I was mistakenly using truthiness to identify empty arrays. It just so happened that the arrays always contained 1 or 0 elements, so it /appeared/ to work except in the rare case of array([0]) where things suddenly exploded.</div><div><br></div><div>I posit that there is no usage of the fact that `bool(array([])) is False` in any real-world code which is not accompanied by a horrible bug writhing in hiding just beneath the surface. For this reason, I wish to see this behavior *abolished*.</div><div><br></div><div>Thank you.</div><div>-Michael<br></div><div><br></div><div><div>Footnotes:<br></div></div><div><div>1: Every now and then, I wish that `ndarray.__{bool,nonzero}__` would just implicitly do `all()`, which would make `if a == b:` work like it does for virtually every other reasonably-designed type in existence.  But then I recall that, if this were done, then the behavior of `if a != b:` would stand out like a sore thumb instead.  Truly, punting on 'any/all' was the right choice.</div><div><br></div><div>2: np.array([[[[]]]]) is also False, which makes this an interesting sort of n-dimensional emptiness test; but if that's really what you're looking for, you can achieve this much more safely with `np.all(x.shape)` or `bool(x.flat)`</div></div></div>
<br>_______________________________________________<br>
NumPy-Discussion mailing list<br>
<a href="mailto:NumPy-Discussion@python.org" target="_blank">NumPy-Discussion@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/numpy-discussion" rel="noreferrer" target="_blank">https://mail.python.org/mailman/listinfo/numpy-discussion</a><br>
<br></blockquote></div><br></div>
_______________________________________________<br>
NumPy-Discussion mailing list<br>
<a href="mailto:NumPy-Discussion@python.org" target="_blank">NumPy-Discussion@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/numpy-discussion" rel="noreferrer" target="_blank">https://mail.python.org/mailman/listinfo/numpy-discussion</a><br>
</blockquote></div>