• Dark Arc@social.packetloss.gg
    link
    fedilink
    English
    arrow-up
    5
    ·
    edit-2
    2 hours 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.

    • AnyOldName3@lemmy.world
      link
      fedilink
      arrow-up
      1
      ·
      21 minutes ago

      The interpreter can’t make the replacement until it’s about to execute the line as __bool__ and __len__ are both (Python’s equivalent of) virtual functions, so it’s got to know the runtime type to know if the substitution is valid. Once it’s that late, it’ll often be faster to execute the requested function than work out if it could execute something faster instead. Even with type hints, it’s possible that a subclass with overridden methods could be passed in, so it’s not safe to do anything until the real runtime type is known.

      Once there’s a JIT involved, there’s an opportunity to detect the common types passed to a function and call specialised implementations, but I don’t think Python’s JIT is clever enough for this. LuaJIT definitely does this kind of optimisation, though.

    • sugar_in_your_tea@sh.itjust.works
      link
      fedilink
      arrow-up
      1
      ·
      edit-2
      46 minutes ago

      Even so, not x is a pretty nice-to-read pattern, and it’s nice that it’s faster than the less nice len(x) == 0. I generally do not care to distinguish whether a list is None or empty, I just want to know if there’s something there. If I care, then I’ll typically separate those checks anyway: if x is None: ..., if len(x) == 0: ....

      • Dark Arc@social.packetloss.gg
        link
        fedilink
        English
        arrow-up
        3
        ·
        39 minutes ago

        I prefer the clarity of len(x) == 0 personally; it’s a pretty low syntactic penalty to clarify intent.

        Obviously, if your variable names are good and you’re consistent about your usage not list can be fine too, but it is a very JavaScript-y thing to me to be that vague and/or rely on “truthiness.”

        • sugar_in_your_tea@sh.itjust.works
          link
          fedilink
          arrow-up
          1
          ·
          23 minutes ago

          The notion of truthiness is defined by the language.

          Here are most of the built-in objects considered false:

          • constants defined to be false: None and False
          • zero of any numeric type: 0, 0.0, 0j, Decimal(0), Fraction(0, 1)
          • empty sequences and collections: ‘’, (), [], {}, set(), range(0)

          It’s not something that happens to work, it’s literally defined that way.

          if not x is the common way to tell if you have data or not, and in most cases, the difference between None and empty data ([], {}, etc) isn’t important.

          len(x) == 0 will raise an exception if you give it None, and that’s usually not what you want. So I guess the verbose way to do that is if x is None or len(x) == 0:, but that’s exactly equivalent to if not x, with the small caveat that it doesn’t check if the value has __len__ implemented. If you’re getting wonky types thrown around (i.e. getting a class instance when expecting a list), you have bigger problems.

          I use type hinting on pretty much all “public” methods and functions, and many of my “private” methods and functions as well. As such, sending the wrong types is incredibly unlikely, so not x is more than sufficient and clearly indicates intent (do I have data?).

  • Kissaki@programming.dev
    link
    fedilink
    English
    arrow-up
    9
    ·
    edit-2
    4 hours 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.

    • sugar_in_your_tea@sh.itjust.works
      link
      fedilink
      arrow-up
      1
      ·
      edit-2
      41 minutes ago

      len(mylist) tells me it’s definitely a list and not null or whatever

      Do you know what else can tell you it’s a list? Type hinting.

      If I care about the distinction between None and an empty list, I’ll make separate checks. len(None) raises an exception, and most of the time that’s not what I want when checking whether there’s something in the list, so not x is generally preferred, especially when type hinting is used consistently enough to be reasonably sure I’m actually getting a list or None. If I get something else, that means things got really broken and they’ll likely get an exception alter (i.e. when doing anything list-like).

      For sequence type objects, such as lists, their truth value is False if they are empty.

      That’s generally exactly what I want. len(x) == 0 doesn’t tell you it’s a list, it just tells you it has __len__. So that could be a dict, list, or a number of other types that have a length, but aren’t lists.

      Don’t rely on checks like that to tell you what type a thing is.

  • Kissaki@programming.dev
    link
    fedilink
    English
    arrow-up
    6
    ·
    4 hours 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?

    • sugar_in_your_tea@sh.itjust.works
      link
      fedilink
      arrow-up
      1
      ·
      edit-2
      34 minutes ago

      “Many” isn’t the same as “most,” though I don’t think there’s any way to know what “most” is.

      But here’s my reason for agreeing w/ the OP:

      • not x checks both None and emptiness, which is usually desired
      • len(x) == 0 will raise an exception if x is null
      • with type hinting, those should be the only two reasonable options

      It’s nice that it’s slightly faster, though performance has absolutely nothing to do w/ my preference, though it does have something to do with my preference for avoiding exceptions for non-exceptional cases.

    • sugar_in_your_tea@sh.itjust.works
      link
      fedilink
      arrow-up
      1
      ·
      31 minutes ago

      Why? not x means x is None or len(x) == 0 for lists. len(x) == 0 will raise an exception if x is None. In most cases, the distinction between None and [] isn’t important, and if it is, I’d expect separate checks for those (again, for explicitness) since you’d presumably handle each case differently.

      In short:

      • if the distinction between None and [] is important, have separate checks
      • if not, not x should be your default, since that way it’s a common pattern for all types
    • sebsch@discuss.tchncs.de
      link
      fedilink
      arrow-up
      7
      arrow-down
      3
      ·
      9 hours ago

      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.

        • sugar_in_your_tea@sh.itjust.works
          link
          fedilink
          arrow-up
          1
          ·
          28 minutes ago

          Because that’s a fundamental aspect of Python. When you’re using a language, you should be familiar with the truthiness values. In Python, it’s pretty sane:

          • [], {}, set(), "", None, False 0 and related values are all “falesy”
          • everything else is truthy

          Basically, if you have non-default values, it’s truthy. Why wouldn’t you trust basic features of the language?

      • flatbield@beehaw.org
        link
        fedilink
        English
        arrow-up
        3
        ·
        edit-2
        6 hours ago

        Compact does not mean easier to understand. They are different tests. The point is, do not make code less readable for speed unless speed matters. I am not going to argue which is more readable in any specific case. That is up to the developer.

      • Nalivai@lemmy.world
        link
        fedilink
        arrow-up
        4
        ·
        edit-2
        8 hours ago

        How many elements in that list? Ah, it’s not list. It’s list, of course, we checked. But it’s not.

    • ExtremeDullard@lemmy.sdf.org
      link
      fedilink
      arrow-up
      4
      ·
      edit-2
      8 hours ago

      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.

    • UndercoverUlrikHD@programming.dev
      link
      fedilink
      arrow-up
      15
      ·
      13 hours ago

      I’d argue that if it’s strict explicitness you want, python is the wrong language. if not var is a standard pattern in python. You would be making your code slower for no good reason.

      • Ephera@lemmy.ml
        link
        fedilink
        English
        arrow-up
        10
        ·
        10 hours 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.

        • fruitcantfly@programming.dev
          link
          fedilink
          arrow-up
          2
          arrow-down
          1
          ·
          8 hours ago

          Containers being “truthy” is quite basic Python and you will find this idiom used in nearly every Python code base in my experience

          • Ephera@lemmy.ml
            link
            fedilink
            English
            arrow-up
            3
            ·
            edit-2
            7 hours 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.

            • fruitcantfly@programming.dev
              link
              fedilink
              arrow-up
              2
              arrow-down
              2
              ·
              edit-2
              7 hours ago

              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@beehaw.org
                link
                fedilink
                English
                arrow-up
                2
                ·
                4 hours 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@lemmy.ml
                link
                fedilink
                English
                arrow-up
                3
                ·
                6 hours 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.