• Ziglin (it/they)@lemmy.world
    link
    fedilink
    English
    arrow-up
    0
    ·
    5 days ago

    Why does not without a parameter return True? I’m starting to like the fact that I haven’t touched python in a while.

    • LostXOR@fedia.io
      link
      fedilink
      arrow-up
      0
      ·
      5 days ago

      I think it’s because not() is equivalent to not(None), and since None is falsy not(None) returns True.

      • takeda@lemm.ee
        link
        fedilink
        arrow-up
        0
        ·
        4 days ago

        Are you sure?

        I can’t test it now, but to me it looks like () is an empty tuple. Python behavior is that for logic operations empty set equals to false. Then we apply not to get True. Not having space between not operator and parentheses makes it look like it is a function.

      • Ziglin (it/they)@lemmy.world
        link
        fedilink
        English
        arrow-up
        0
        ·
        5 days ago

        Why is literally nothing equivalent to None? Is it because None is the default value of an optional parameter? (If so why oh why is it optional)

        • spooky2092@lemmy.blahaj.zone
          link
          fedilink
          English
          arrow-up
          0
          ·
          edit-2
          4 days ago

          Because nothing isn’t something, and something is true. It’s base Boolean logic where everything is either true or false. Null/nothing is false.

          It’s a weird way to think about conditionals, but it makes sense when you use them in real examples. In my case, I use them like this when I need to make sure that a variable has a value. So I can do something like

          If(variable){do things with the variable}else{do stuff when the variable doesn’t exist}

            • spooky2092@lemmy.blahaj.zone
              link
              fedilink
              English
              arrow-up
              0
              ·
              4 days ago

              No it’s not, “” (a null/empty string) is the parameter. Not every function needs a parameter to be valid, and negation is one of them. Negating nothing is something, so “not()” = “not(null)” = “not(false)” = “true”

        • Pup Biru@aussie.zone
          link
          fedilink
          English
          arrow-up
          0
          ·
          5 days ago
          a  = null
          if not a:
             
          

          if not a were null then an if that evaluates that would evaluate it as falsy… also if a would evaluate as falsy :/ that’s far weirder behaviour