• UndercoverUlrikHD@programming.dev
      link
      fedilink
      arrow-up
      0
      ·
      2 months 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
        0
        ·
        2 months 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
          1
          ·
          2 months 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
            0
            ·
            edit-2
            2 months 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
              1
              ·
              edit-2
              2 months 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

    • ExtremeDullard@lemmy.sdf.org
      link
      fedilink
      arrow-up
      0
      ·
      edit-2
      2 months 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.

    • sugar_in_your_tea@sh.itjust.works
      link
      fedilink
      arrow-up
      0
      ·
      2 months 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
            • sugar_in_your_tea@sh.itjust.works
              link
              fedilink
              arrow-up
              0
              ·
              edit-2
              2 months ago

              def some_func(*args, kwarg=[])

              Don’t do this:

              def fun(l=[]):
                  l.append(len(l))
                  return l
              
              fun()  # [0]
              fun()  # [0, 1]
              fun(l=[])  # [0]
              fun()  # [0, 1, 2]
              fun(l=None)  # raise AttributeError or TypeError if len(l) comes first
              

              This can be downright cryptic if you’re passing things dynamically, such as:

              def caller(*args, **kwargs):
                  fun(*args, **kwargs)
              

              It’s much safer to do a simple check at the beginning:

              if not l: 
                  l = []