• Kissaki ( Kissaki@programming.dev ) 
    link
    fedilink
    English
    arrow-up
    10
    ·
    2 years ago

    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.

    • Michal ( Michal@programming.dev ) 
      link
      fedilink
      arrow-up
      4
      ·
      2 years ago

      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.