<div dir="ltr">> More importantly though, allowing your proposed semantics would cause a lot of silent bugs in code like `if arr == value`, which would be silently true of array inputs. We already diverge from python on what == means, so I see no reason to match the normal semantics of bool.<div><br></div><div>Eric hits the nail right on the head here. (er, ahh, you're both Eric!)</div><div>And this gets worse; not only would `a == b` be true, but so would `a != b`! For the vast majority of arrays, `bool(x != x)` would be True!</div><div><br></div><div>I can resonate with Eric F's feelings, because to be honest, I've never been a big fan of the fact that comparison operators return arrays in the first place.  That said... it's a difficult design question, and I can respect the decision that was made; there certainly are a large variety of circumstances where broadcasting these operations are useful. On the other hand, it is a decision that comes with implications that cannot be ignored in many other parts of the library, and truthiness of arrays is one of them.</div><div><div class="gmail_extra"><div class="gmail_quote"><div><br></div><div>> I'd be tentatively in favor of deprecating bool(array([1]) with a warning asking for `.squeeze()` to be used, since this also hides a (smaller) class of bugs.</div><div><br></div><div>I can get behind this as well, though I just keep wondering in the back of my mind whether there's some tricky but legitimate use case that I'm not thinking about, where arrays of size 1 just happen to have a natural tendency to arise.</div><div><div dir="ltr"><br class="gmail-Apple-interchange-newline">On Sat, Aug 19, 2017, 10:34 Eric Firing <<a href="mailto:efiring@hawaii.edu" target="_blank">efiring@hawaii.edu</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">On 2017/08/18 11:45 AM, Michael Lamparski wrote:<br>> Greetings, all.  I am troubled.<br>><br>> The TL;DR is that `bool(array([])) is False` is misleading, dangerous,<br>> and unnecessary. Let's begin with some examples:<br>><br>>  >>> bool(np.array(1))<br>> True<br>>  >>> bool(np.array(0))<br>> False<br>>  >>> bool(np.array([0, 1]))<br>> ValueError: The truth value of an array with more than one element is<br>> ambiguous. Use a.any() or a.all()<br>>  >>> bool(np.array([1]))<br>> True<br>>  >>> bool(np.array([0]))<br>> False<br>>  >>> bool(np.array([]))<br>> False<br>><br>> One of these things is not like the other.<br>><br>> The first three results embody a design that is consistent with some of<br>> the most fundamental design choices in numpy, such as the choice to have<br>> comparison operators like `==` work elementwise.  And it is the only<br>> such design I can think of that is consistent in all edge cases. (see<br>> footnote 1)<br>><br>> The next two examples (involving arrays of shape (1,)) are a<br>> straightforward extension of the design to arrays that are isomorphic to<br>> scalars.  I can't say I recall ever finding a use for this feature...<br>> but it seems fairly harmless.<br>><br>> So how about that last example, with array([])?  Well... it's /kind of/<br>> like how other python containers work, right? Falseness is emptiness<br>> (see footnote 2)...  Except that this is actually *a complete lie*, due<br>> to /all of the other examples above/!<br><br>I don't agree.  I think the consistency between bool([]) and<br>bool(array([])) is worth preserving.  Nothing you have shown is<br>inconsistent with "Falseness is emptiness", which is quite fundamental<br>in Python.  The inconsistency is in distinguishing between 1 element and<br>more than one element.  To be consistent, bool(array([0])) and<br>bool(array([0, 1])) should both be True.  Contrary to the ValueError<br>message, there need be no ambiguity, any more than there is an ambiguity<br>in bool([1, 2]).<br><br>Eric<br>______________________________<wbr>_________________<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/<wbr>mailman/listinfo/numpy-<wbr>discussion</a></blockquote></div></div></div></div></div>