[Numpy-discussion] Why are empty arrays False?

Chris Barker chris.barker at noaa.gov
Tue Aug 22 12:31:43 EDT 2017


On Mon, Aug 21, 2017 at 7:34 AM, Benjamin Root <ben.v.root at gmail.com> wrote:

> I've long ago stopped doing any "emptiness is false"-type tests on any
> python containers when iterators and generators became common, because they
> always return True.
>

good point.

Personally, I've thought for years that Python's "Truthiness" concept is a
wart. Sure, empty sequences, and zero values are often "False" in nature,
but truthiness really is application-dependent -- in particular, sometimes
a value of zero is meaningful, and sometimes not.

Is it really so hard to write:

if len(seq) == 0:

or

if x == 0:

or

if arr.size == 0:

or

arr.shape == (0,0):

And then you are being far more explicit about what the test really is.

And thanks Ben, for pointing out the issue with iterables. One more example
of how Python has really changed its focus:

Python 2 (or maybe, Python1.5) was all about sequences. Python 3 is all
about iterables -- and the "empty is False" concept does not map well to
iterables....

As to the topic at hand, if we had it to do again, I would NOT make an
array that happens to hold a single value act like a scalar for bool() -- a
1-D array that happens to be length-1 really is a different beast than a
scalar.

But we don't have it to do again -- so we probably need to keep it as it is
for backward compatibility.

-CHB



-- 

Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R            (206) 526-6959   voice
7600 Sand Point Way NE   (206) 526-6329   fax
Seattle, WA  98115       (206) 526-6317   main reception

Chris.Barker at noaa.gov
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20170822/f938035a/attachment.html>


More information about the NumPy-Discussion mailing list