On Mon, Aug 23, 2021 at 6:54 AM Thomas Grainger <tagr...@gmail.com> wrote:

> here's another fun one "A False midnight":
> https://lwn.net/Articles/590299/
> https://bugs.python.org/issue13936#msg212771


This is a great example of the problem of the assumption of zero as
representing false.

I’ve always been ambivalent about Python’s concept of Truthiness
(“something or nothing”). If I were to write my own language, I would
probably require a actual Boolean for, eg, an if statement.

The fact is that what defines falsiness is use case dependent. Numbers are
the best example, zero. A often be a perfectly meaningful number.

Which id why Brandon suggested that testing the length of a sequence was a
good way to be explicit about what you mean by false in a particular
context.

But I see no reason to add a standardized way to check for an empty
container- again “emptiness” may not be obviously defined either.

Numpy arrays, (or Pandas Dataframes) are a good example here — there are
more than one way to think of them as false - but maybe more than one way
to think of them as empty too:

Is a shape () (scalar) array empty ?

Or shape (100, 0) ?

Or shape (0, 100)

Or shape (0,0,0,0)

Or a rank 1 array that’s all zeros? Or all false?

Anyway, the point is that “emptiness” may be situation specific, just like
truthiness is.

So explicitly specifying that you are looking for len(container) == 0 is
more clear than isempty(container) would be, even  if it did exist.

With duck typing, you may well not know what type you are dealing with, but
you absolutely need to know how you expect  that object to behave in the
context of your code. So if having a zero length is meaningful in your code
— then only objects with a length will work, which is just fine.

-CHB



_______________________________________________
> Python-ideas mailing list -- python-ideas@python.org
> To unsubscribe send an email to python-ideas-le...@python.org
> https://mail.python.org/mailman3/lists/python-ideas.python.org/
> Message archived at
> https://mail.python.org/archives/list/python-ideas@python.org/message/IFVHWIOEYST34IVQVOYX7UA45AU6EQ4O/
> Code of Conduct: http://python.org/psf/codeofconduct/
>
-- 
Christopher Barker, PhD (Chris)

Python Language Consulting
  - Teaching
  - Scientific Software Development
  - Desktop GUI and Web Development
  - wxPython, numpy, scipy, Cython
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/RDWNT6S2F6CBMNHFGD2LT2CJHVOTFXR4/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to