• Ephera ( Ephera@lemmy.ml ) 
        link
        fedilink
        English
        arrow-up
        11
        ·
        2 years ago

        You always want explicitness when programming. Not everyone reading your code will be deep into Python and relying on falsiness makes it harder to understand.

        • While I agree to some extent, if not var is more than clear enough for anyone that knows python. If that pattern confuses someone, they probably aren’t at level where they should be dealing with python at a professional level. The same way you would expect people to understand pointers and references before delving into C code.

          This sort of stuff is something I taught first year engineering student in their programming introductory course, it’s just how python is written.

          For what it’s worth, it’s sort of similar in Erlang/Elixir. If you want to check if a list is empty, checking the length of the list is discouraged. Instead you would use Enum.empty?().

          • Ephera ( Ephera@lemmy.ml ) 
            link
            fedilink
            English
            arrow-up
            2
            ·
            2 years ago

            Well, it certainly wouldn’t be the first time that I call some code unnecessarily hard to read and others call it pythonic.

            I understand that truthiness has an advantage when you don’t have static types, because it deals somewhat reasonably with most types, and then that is why many experienced Python programmers tend to use it. But I maintain the position that it therefore necessarily also makes it harder to read the code, because you necessarily provide the reader with fewer hints as to what is actually in that variable. It is very much like code using lots of generics in statically typed code.

            As for if Enum.empty?(var), I actually prefer that to checking the length. That syntax in particular, I find a bit too complex – my preferred version is if var.is_empty() – but I still think it makes it easier to read than a length check.
            Of course, there’s the nuance that it’s more syntax to remember for actually writing the code. And in particular in dynamically typed languages, your editor may not be able to auto-complete that, so I can understand just not bothering with the extra syntax and doing len == 0 instead. It’s fine.

          • Ephera ( Ephera@lemmy.ml ) 
            link
            fedilink
            English
            arrow-up
            3
            ·
            2 years ago

            Yeah, I’m talking less deep than that. Plenty programming beginners will be reading Python code. And personally, I’m a fulltime software engineer, but just don’t do much Python, so while I had it in the back of my mind that Python does truthiness, I would have still thought that var must be a boolean, because it’s being negated. Obviously, a different variable name might’ve given me more of a clue, but it really doesn’t reduce mental complexity when I can’t be sure what’s actually in a variable.

            • But if those beginners want to stop being beginners, then they must learn the basics of the language. It makes no more sense to demand that everyone who programs in Python caters to beginners, than it makes to demand that everyone writing in English write at a 3rd grade reading level for the sake of English language learners

              • flatbield ( furrowsofar@beehaw.org ) 
                link
                fedilink
                English
                arrow-up
                4
                ·
                2 years ago

                I think you just like the truth test. Frankly I have used Python for over 25 years and have been doing programming for almost 50 years in many different languages. If you think I am somehow a beginner, I would disagree. The truth test is just like so many other Python specific idioms that grow in number by the year. They are not at all obvious unless you are deep into Python. Moreover, the truth test and the len() test are not the same test. One might be able to use either in a specific case, but that is case specific and which is more readable is up to the developer and we may well disagree on that choice. The other consideration in Python, speed of writing code which is often why many of us use Python and that may lead to different choices too including based on habit.

                Lot of this reminds me of the Pascal vs. C debate of the 1980’s. Pascal was all about readability over compactness. C on the other hand, seemed to attract people that loved to write very compact code that was almost impossible to figure out on first glance. Me personally, I guess I’d choose C over Pascal but write the C in more of a Pascal authoring philosophy. Similarly, with Python, I often do not go for all of the Python idioms. Lot of that is just writing what I am thinking, and the rest is probably habit. If I am going to test 0 length then I’ll probably test zero length. I do not find it at all obvious that, oh, I want to test 0 length so the Python idiom for that is to truth test. I absolutely know that to be the case on certain types of objects, but it probably is not going to be my first choice.

              • Ephera ( Ephera@lemmy.ml ) 
                link
                fedilink
                English
                arrow-up
                2
                ·
                2 years ago

                Yeah, my stance for both of those is the same: If the complexity aids you in communicating better, then use it. But if you’re using big words where small words would do, then you’re doing a disservice to your readers.

    • I never understood that argument. If you can be sure the type is a collection (and this you always should) not list is so moch easier to read and understood than the length check.

    • In complex cases where speed is less important than maintainability, I tend to agree.

      In this case, a simple comment would suffice. And in fact nothing at all would be okay for any half-competent Python coder, as testing if lists are empty with if not is super-standard.

  • 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.

  • Dark Arc ( Dark_Arc@social.packetloss.gg ) 
    link
    fedilink
    English
    arrow-up
    7
    ·
    2 years ago

    I don’t know about others … but I’m not using Python for execution speed.

    Typically the biggest problem in a program is not 100 million calls of len(x) == 0. If it was, the interpreter could just translate that expression during parsing.

    • This. I rarely boot up Python for the tasks I need to do, and if they are, they are one of the following:

      • Assistant code for other coding language
      • Throwaway script
      • Prototype before using a faster language
      • Assuming an equivalent package is produced, what’s the maintenance cost (factoring in coder availability) difference between the Python vs faster language implementations?

        ^^ therein lies the rub

        Reminds of the expression, premature optimization is the root of all evil

        if not swimming in funding, might be a darwinic move to choose the faster language and have to code everything yourself from scratch

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

    Makes perfect sense. If you’re checking if a collection is empty you don’t need to know its exact size. Getting the size can be very inefficient in collections like linked lists or trees, if you have to follow all nodes. To check if it’s empty, all you need fo know if at least one item exists. If one does, there’s no point counting the rest.

    People who don’t understand the difference will probably not understand the difference between passing a list and passing an literator/generator to any() .

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

    While the 2nd approach is not wrong, the first method is considered more Pythonic. Many people don’t agree, but I’ve already put forward my points in a previous article on that debate.

    Does Pythonic mean best practice in the python community or “good python”?

    If “many people don’t agree”, how can they claim it to be “pythonic”? Isn’t that contradictory?