Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Brite
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Brand Logo

agnos.is Forums

  1. Home
  2. Programmer Humor
  3. Modern Programming

Modern Programming

Scheduled Pinned Locked Moved Programmer Humor
programmerhumor
74 Posts 46 Posters 2 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • kn0wmad1c@programming.devK [email protected]

    num % 2 isn't a boolean result in any of these languages, so I feel like it would always output "odd"

    Edit: 0 is false, everything else is true.

    sleeplessone@lemmy.mlS This user is from outside of this forum
    sleeplessone@lemmy.mlS This user is from outside of this forum
    [email protected]
    wrote on last edited by
    #22

    In JS at least, there's a concept of truthiness and falsiness. 0, undefined, null, and a few other non-boolean values are treated as false if used in conditionals and logical operations, while every other value is treated as true. I'm pretty sure python has something similar.

    H 1 Reply Last reply
    4
    • K [email protected]

      Why is the return first?

      J This user is from outside of this forum
      J This user is from outside of this forum
      [email protected]
      wrote on last edited by
      #23

      I think the idea is it reads more naturally, so you can read it like this return A if statement is true else return B

      N 1 Reply Last reply
      16
      • entropicdrift@lemmy.sdf.orgE [email protected]
        print("odd" if num % 2 else "even")
        

        That's the native python version, for those curious

        D This user is from outside of this forum
        D This user is from outside of this forum
        [email protected]
        wrote on last edited by
        #24

        The ternary syntax is really my only real gripe with python design -- putting the conditional BETWEEN the true and false values feels so very messy to me.

        rbos@lemmy.caR B idunnololz@lemmy.worldI 3 Replies Last reply
        83
        • K [email protected]

          Why is the return first?

          B This user is from outside of this forum
          B This user is from outside of this forum
          [email protected]
          wrote on last edited by [email protected]
          #25

          Edit... I reread your comment and realized that python does it differently and that everything I typed was irrelevant... I'm still gonna leave it if anyone is interested in ternary expressions, but I suppose the answer to your question is, that's just how python does it.

          That's how ternary operators are designed to work. In essence, if you're looking to do a single line if/then, you can directly assign a variable from the result of a ternary expression.

          As an example, I was scripting something earlier where there may or may not be a value returned from a function, but I still had to do something with that return value later. For this thing, I was using JavaScript.

          I ended up with:

          return platform == "name"  ? "Option 1" : "Option 2"
          

          If I were to write that out in a typical if/then it would be:

          if (platform == "name") {
              return "option 1"
          } else {
              return "option 2"
          }
          
          

          A ternary starts with a boolean expression, then the if true value, else the false value. That's returned to either a variable or if in a function like my example, to the object calling the function. It's just a way to write less code that in many cases is easier to read.

          1 Reply Last reply
          8
          • P [email protected]

            Yeah... I played that "serial killer or programming language inventor" game.

            The only one I was completely in disagreement with was the inventor of Python. He's definitely a mass-murderer

            C This user is from outside of this forum
            C This user is from outside of this forum
            [email protected]
            wrote on last edited by [email protected]
            #26

            Are you sure it isn't just that he's Dutch?

            1 Reply Last reply
            4
            • D [email protected]

              The ternary syntax is really my only real gripe with python design -- putting the conditional BETWEEN the true and false values feels so very messy to me.

              rbos@lemmy.caR This user is from outside of this forum
              rbos@lemmy.caR This user is from outside of this forum
              [email protected]
              wrote on last edited by
              #27

              It's kinda natural to me having used Perl a lot.

              S L 2 Replies Last reply
              12
              • R [email protected]

                Python does that, too.

                https://docs.python.org/3/library/stdtypes.html#boolean-operations-and-or-not

                satyrsack@lemmy.sdf.orgS This user is from outside of this forum
                satyrsack@lemmy.sdf.orgS This user is from outside of this forum
                [email protected]
                wrote on last edited by
                #28

                Are you just referring to how Python uses the English and/or instead of the more common &&/||? I think what the user above you was talking about was Lua's strange ternary syntax using and/or.

                lime@feddit.nuL 1 Reply Last reply
                1
                • kn0wmad1c@programming.devK [email protected]

                  Ah that makes sense.

                  satyrsack@lemmy.sdf.orgS This user is from outside of this forum
                  satyrsack@lemmy.sdf.orgS This user is from outside of this forum
                  [email protected]
                  wrote on last edited by
                  #29

                  It doesn't make sense. I understand it, but it doesn't make sense.

                  L 1 Reply Last reply
                  1
                  • entropicdrift@lemmy.sdf.orgE [email protected]

                    Python is kinda like that in general, unless you try to make it read like ass

                    W This user is from outside of this forum
                    W This user is from outside of this forum
                    [email protected]
                    wrote on last edited by
                    #30

                    You would not believe the number of people I’ve interviewed who excel at making Python read like ass.

                    entropicdrift@lemmy.sdf.orgE 1 Reply Last reply
                    4
                    • W [email protected]

                      You would not believe the number of people I’ve interviewed who excel at making Python read like ass.

                      entropicdrift@lemmy.sdf.orgE This user is from outside of this forum
                      entropicdrift@lemmy.sdf.orgE This user is from outside of this forum
                      [email protected]
                      wrote on last edited by
                      #31

                      I mean, it does have enough ways to write the same thing that it can really allow for some funny code golf, but some people just have no sense of readability whatsoever.

                      1 Reply Last reply
                      5
                      • rbos@lemmy.caR [email protected]

                        It's kinda natural to me having used Perl a lot.

                        S This user is from outside of this forum
                        S This user is from outside of this forum
                        [email protected]
                        wrote on last edited by
                        #32

                        That's not quite the argument you might think it is

                        rbos@lemmy.caR 1 Reply Last reply
                        18
                        • S [email protected]

                          That's not quite the argument you might think it is

                          rbos@lemmy.caR This user is from outside of this forum
                          rbos@lemmy.caR This user is from outside of this forum
                          [email protected]
                          wrote on last edited by
                          #33

                          Argument?

                          S 1 Reply Last reply
                          3
                          • maven@lemmy.zipM [email protected]
                            This post did not contain any content.
                            mdhughes@lemmy.sdf.orgM This user is from outside of this forum
                            mdhughes@lemmy.sdf.orgM This user is from outside of this forum
                            [email protected]
                            wrote on last edited by
                            #34

                            print( ["even", "odd"][num % 2] )

                            If you need to avoid evaluating the wrong branch:

                            print( [lambda: "even", lambda: "odd"][num % 2]() )

                            1 Reply Last reply
                            36
                            • maven@lemmy.zipM [email protected]
                              This post did not contain any content.
                              E This user is from outside of this forum
                              E This user is from outside of this forum
                              [email protected]
                              wrote on last edited by
                              #35

                              For optimal performance, you should rewrite it in Rust:

                              inline_python::python! {
                                  print(js2py.eval_js("(number) => number % 2 ? 'odd' : 'even'")(number))
                              };
                              
                              A 1 Reply Last reply
                              115
                              • rbos@lemmy.caR [email protected]

                                Argument?

                                S This user is from outside of this forum
                                S This user is from outside of this forum
                                [email protected]
                                wrote on last edited by
                                #36

                                You know, the stuff in @_

                                rbos@lemmy.caR 1 Reply Last reply
                                9
                                • satyrsack@lemmy.sdf.orgS [email protected]

                                  Are you just referring to how Python uses the English and/or instead of the more common &&/||? I think what the user above you was talking about was Lua's strange ternary syntax using and/or.

                                  lime@feddit.nuL This user is from outside of this forum
                                  lime@feddit.nuL This user is from outside of this forum
                                  [email protected]
                                  wrote on last edited by [email protected]
                                  #37

                                  no, the linked table shows how python also returns the first non-falsey result of an a or b expression rather than just giving a boolean. it's useful for initialising optional reference args:

                                  def foo(a: list = None)
                                      a = a or []
                                  

                                  works with and as well.

                                  1 Reply Last reply
                                  4
                                  • E [email protected]

                                    For optimal performance, you should rewrite it in Rust:

                                    inline_python::python! {
                                        print(js2py.eval_js("(number) => number % 2 ? 'odd' : 'even'")(number))
                                    };
                                    
                                    A This user is from outside of this forum
                                    A This user is from outside of this forum
                                    [email protected]
                                    wrote on last edited by [email protected]
                                    #38

                                    And now you can use wasm to run it in a browser!

                                    V 1 Reply Last reply
                                    66
                                    • A [email protected]

                                      And now you can use wasm to run it in a browser!

                                      V This user is from outside of this forum
                                      V This user is from outside of this forum
                                      [email protected]
                                      wrote on last edited by
                                      #39

                                      Full circle 😆

                                      U 1 Reply Last reply
                                      22
                                      • S [email protected]

                                        Please. That's C's ternary operator. JS is just a pile of garbage cosplaying as a programming language

                                        V This user is from outside of this forum
                                        V This user is from outside of this forum
                                        [email protected]
                                        wrote on last edited by
                                        #40

                                        Why do you say it's a pile of garbage?

                                        S 1 Reply Last reply
                                        3
                                        • D [email protected]

                                          The ternary syntax is really my only real gripe with python design -- putting the conditional BETWEEN the true and false values feels so very messy to me.

                                          B This user is from outside of this forum
                                          B This user is from outside of this forum
                                          [email protected]
                                          wrote on last edited by [email protected]
                                          #41

                                          Eh, reads pretty naturally to me. That said, (like I lisp)

                                          rovingnothing29@lemmy.worldR S 2 Replies Last reply
                                          17
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          • Login

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • World
                                          • Users
                                          • Groups