4 ms·
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 empt
by int_19h 2y 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.
- adamc 2y agoI don't agree with this at all, and I wonder if it reflects what other languages you use that may have shaped your assumptions. `if mylist` feels very much like Common Lisp to me. In much of the code I've seen, the value would never be none because an empty list was definitely created.
- Joker_vD 2y agoAh yes, reusing an empty list NIL as the false value because having a separate #f atom is bad for whatever reason.
- adamc 2y agoIt is very convenient and makes the code terse and that matters.
- Joker_vD 2y agoMay I suggest J (or K) to you then? It has even more conveniences, and allows for much terser code than LISP — even its name is terser! And has even less extraneoues syntax: did you know that parentheses can also be dispensed with? It's true!
- kazinator 2y agoBut it's only one value in the entire system. There is only one nil; there is no other false but nil. And there is no other empty list but nil. An empty array or string are not false. Zero is not false. In Lisp dialects with nil, we don't have endless discussions about how to test for false, or for empty, as the case may be.
- Joker_vD 2y ago> And there is no other empty list but nil. Okay? That still doesn't justify using it as replacement for #f (which is also a unique value). Or as #t for that matter, there is also only one #t in the entire system. > An empty array or string are not false. Zero is not false. And an empty list is also not false, only #f is false. So simple and uniform! Wait, there is no #f, but an empty list is false? Why? Why is empty list so special? Or, alternatively, why are you using the false value as an empty list? The idea to represent a list as [1 | [2 | [3 | False]]] is quite strange. Just spare an atom, it's only what, eight bytes nowadays? > how to test for false In sane programming languages, the booleans are self-testing, so to speak, and they are the only values with such property, so: if not val then ... (if (not val) ... tests for false. > or for empty if val_specific_is_empty(val) then ... (if (null? val) ... But of course, it only matters for languages that have other options for building composite data types than CONS-ing two items together.
- kazinator 2y agoNo language with completely separate booleans has ever had a fun club. (Scheme isn't that language, because everything that is not #f is true.) List processing is cumbersome in Scheme because the empty list is't false, and because car and cdr cannot be applied to (). (Which, stupidly, isn't even self-evaluating; you have to use '()). The empty list is special because once upon a time lisp emphasized list processing more. Complex list processing is still important in metaprogramming, and representations of flexible dynamic data sets. You're not working with lists, it utterly doesn't matter that nil also has a role as the empty list. Just like you don't care that your physician also plays saxophone in a jazz band; he's not doing it during your appointment.
- wenc 2y agoFor me, len() is much less ambiguous. Also in Pandas if you try to check the truth value of a dataframe to see if it’s empty, it will fail. It will say “the truth value of a dataframe is ambiguous”. df.empty is less ambiguous but you have to remember it specifically for dataframes. But len(df) > 0 almost always works for any type of collection.