On Sun, Aug 22, 2021 at 10:28 PM Tim Hoffmann via Python-ideas
<python-ideas@python.org> wrote:
>
> Hi all,
>
> The Programming Recommendations section in PEP-8 states
>
> "For sequences, (strings, lists, tuples), use the fact that empty sequences 
> are false:"
>
>   # Correct:
>   if not seq:
>   if seq:
>
>   # Wrong:
>   if len(seq):
>   if not len(seq):
>
> In the talk "When Python Practices Go Wrong" Brandon Rhodes makes a good 
> point against this practice based on "explicit is better than implicit" 
> (https://youtu.be/S0No2zSJmks?t=873). He advertizes using
>
>   if len(seq):
>
> While that is as explicit as one can get within the current language, it 
> could still be more explicit: Semantically, we're not interested in the 
> (zero) length of the sequence, but want to know if it is empty.
>
>
> **Proposal**
>
> Therefore, I propose syntax for an explicit empty check
>
>   if isempty(seq):   (i)
>
> or
>
>   if seq.is_empty()  (ii)
>
> This proposal is mainly motivated by the Zen verses "Explicit is better than 
> implicit" and "Readability counts".
>

I don't see that this gives anything above len(seq). Taking an example
from the video you linked to:

def unfriend(subject, users):
    if not users:
        return
    remove_edges('friend', subject, users)

"What is the type of users? The only hint you are given is that it is
used in an 'if' statement."

Not true. You also have the fact that the name "users" is a plural.
Based on that, and that alone, I would assume that it is some sort of
collection. And then he goes on to say that it could be the integer
12. Okay, sure. But if you care about that distinction, "if
len(users):" is absolutely fine here, and proves that your function
will break if given an integer number of users rather than a
collection of them.

In Python, isempty(x) is spelled bool(x). That's simply what it means.
I ask you, for these types, what should isempty do, what does bool do,
and what does len do?

* datetime.timedelta
* range
* slice
* an SQL query that has yet to be executed
* an SQL result set

Under what circumstances would bool(x) differ from isempty(x)? Under
what circumstances should the distinction be made? And when it should
be made, would bool(len(x)) be different from isempty(x)?

To be quite honest, my usual answer to "What is the type of X?" is "I
don't care" (or "I don't care, as long as it is <quality>" eg iterable
or subscriptable etc). The unfriend function shouldn't need to care
what kind of thing it's been given, but if it does, Python has these
things called type hints that can provide extra information to a
static analyzer (and also to a human who's reading the code). But
static analysis is often smart enough to not need them - and, quite
frankly, static analysis is pretty near to magic with its ability to
sniff out problems that programmers wouldn't even have thought to
check for. (I've seen some Coverity reports and been pretty astonished
at its detail.)

What's the problem being solved by isempty? Are there any situations
that couldn't be solved by either running a type checker, or by using
len instead of bool?

ChrisA
_______________________________________________
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/WMKVWRG2M7DPLGVKWBGZFMW3VOMHYF7D/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to