The Pythonic Emptiness

(blog.codingconfessions.com)

Comments

int_19h 7 hours ago
I don't find any of the arguments here particularly convincing. This claim in particular is weird:

> Similarly, when you use len() to check a sequence for emptiness, you are reaching out for a more powerful tool than necessary. As a result it may make the reader question the intent behind it, e.g. if there is a possibility of handling objects other than a built-in sequence type here?

Given that checking for truthiness is less strict than a length test, by the same token, whenever you use it, you're reaching for an even more powerful tool than necessary. And, if anything, seeing `not items` is what makes me question the intent - did the author mean to also check for None etc here, or are they just assuming that it's never going to be that? And sure, well-written code will have other checks and asserts about it - but when I'm reading your code, I don't know if it's missing an assert like that because you intended it, or because you couldn't be bothered to write it.

OTOH len() is very explicit about what is actually checked, and will fail fast and loudly if the argument is not actually a sequence.

Also note that it's not, strictly speaking, an either-or - you can use `not len(x)` instead of `len(x) == 0` if you want a distinctive pattern specifically for empty collection checks.

lihaoyi 7 hours ago
Python's truthiness behavior was the trigger for one of my worst ever bugs early in my career, which not only pulled in senior engineering but also marketing/comms and legal to help sort out the mess. Not a fan!
sjsdaiuasgdia 7 hours ago
I'm mostly annoyed that 'if len(items) == 0' / 'if len(items) > 0' aren't presented as options.

If we're talking about readability, they're far clearer than either of the options in the article and require no pre-knowledge of truthiness rules.

mulmboy 7 hours ago
`if x: foo()` is a cancer on the Python community. Devs often use it with the intention of handling x being None, and carelessly lump in zero and empty lists/strings at the same time. Endless bugs.
James_K 7 hours ago
Isn't this just a Perl feature (arrays are also their length and zero values are falsy)? I can't help but feel Python is getting closer to Perl as time goes on. Ironic, since their original goal was to be simple and make themselves distinct from Perl. What was the saying again? "There should be one-- and preferably only one --obvious way to do it." Honestly, I think it was all this "Zen" stuff that lead Python down the path to weirdness. This article reads like a monk interpreting sacred text. I can think of no good reason for all this malarkey.
FridgeSeal 5 hours ago
Python’s “truthiness” is a cutesy feature that is just an excuse for bugs in your code. It’s opaque/ too magic, exhibits poor readability and endless confusion.

Just use a normal check, like everyone is expecting to see.

“Oh but what if it’s not a sequence”, well then you have bigger problems. Why are you emptiness testing something that may-or-may-not-be-a-sequence? Maybe solve that problem first.

tromp 7 hours ago
I would prefer a standard list method/function to test for emptiness, which would be both readable and efficient.
morkalork 7 hours ago
Holy moly, that meme about type checkers and variable names, someone is arguing for hungarian notation in 2024?!
JackSlateur 10 November 2024
tldr: do not use 'if len(list) == 0', use 'if not list' !

You just have to not write any bug in your code. Also, use type checking everywhere. And rewrite your mind, too.

The benefits are worth it .. ! Oh well;

ReflectedImage 10 November 2024
Use the simplest syntax and check the code works via unit testing like you should be doing. Don't statically type your code as it increases the bugs by a factor of 3x typically.