One of the principles of the Pythonic style is: simple is better than complex.
But is it when you consider ambiguity and expressiveness too?
len(mylist) tells me it’s definitely a list and not null or whatever. It also tells me the intent of whoever writes the code was checking length.
If it requires a long article to explain, I’d certainly be hesitant to use and prefer. The question is, is it surprising or intuitive that the truthy becomes a length check. What do you want to express with your code. And who will read it, and what expectations and requirements do you want to require of them.
For sequence type objects, such as lists, their truth value is False if they are empty.
It’s just how pythonic code is written. All python developers know that not and bool can indicate that variable is either empty or null. They will also use the in operator and other python specific syntax.
But is it when you consider ambiguity and expressiveness too?
len(mylist)tells me it’s definitely a list and not null or whatever. It also tells me the intent of whoever writes the code was checking length.If it requires a long article to explain, I’d certainly be hesitant to use and prefer. The question is, is it surprising or intuitive that the truthy becomes a length check. What do you want to express with your code. And who will read it, and what expectations and requirements do you want to require of them.
It’s just how pythonic code is written. All python developers know that
notandboolcan indicate that variable is either empty or null. They will also use theinoperator and other python specific syntax.