On 2021-08-23 at 21:06:46 -0000,
Tim Hoffmann via Python-ideas
- Re bool: As experienced python users we are used to translate `if not users` to "if users is empty" or "if we have no users", but it *is* less explicit than `if users.is_empty()`.
I mentally translate "if not users" to "if there are not users" or "if there are no users." Whether users is a list (or some other sequence), a dict (or some other mapping), a set, or even some oddball collection type (e.g., the responses from a database query) is an implementation detail about which I don't care at that point.
- Re len: `if not len(users)` or `if len(users) == 0` is more explicit, but its semantically on a lower level. Counting elements is a more detailed operation than only checking if we have any element. That detail is not needed and distracting if we are only interested in is_empty. This is vaguely similar to iterating over indices (`for i in range(len(users))`) vs. iterating over elements (`for user in users`). We don't iterate over indices because that's usually a detail we don't need.
Exactly. Asking whether a collection contains an element is on a slightly lower level than asking whether or not "there are any [Xs]."