• maegul
    link
    fedilink
    English
    arrow-up
    13
    arrow-down
    1
    ·
    edit-2
    7 months ago

    Python, checking in …

    return (a or b)
    

    Parentheses aren’t necessary, I just prefer them for readability.

    See python documentation on boolean operators for reference. Short story is a or b is an expression that evaluates to a if a is “truthy” else b. “Falsy” is empty strings/containers, 0 and None.

    • juggling_collie
      link
      fedilink
      arrow-up
      13
      ·
      7 months ago

      You need to be careful here though. You might not intend for 0 and None to mean the same thing.

      • Bumblefumble@lemm.ee
        link
        fedilink
        arrow-up
        3
        ·
        edit-2
        7 months ago

        So this won’t do the intended thing if a is 0.

        Edit: Sorry I meant to reply to the parent comment, realising now you already write the exact same thing.

        • maegul
          link
          fedilink
          English
          arrow-up
          1
          ·
          7 months ago

          Actually not sure what you mean by this. By my reckoning, 0 is definitely falsy in Python such that a or b will evaluate to b if a is zero.

      • maegul
        link
        fedilink
        English
        arrow-up
        1
        ·
        7 months ago

        Well that’s part of the fun, right!?

        In all seriousness I very much dislike that zero is falsy in Python. It seems to me illogical and “cute”. Zero is a number much more than an empty container is a container.

      • maegul
        link
        fedilink
        English
        arrow-up
        1
        ·
        7 months ago

        That’s a shame, it would have been fitting in “modern” Python along with the walrus and static type system.