python syntax for conditional is unfortunate

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Neal Becker

    python syntax for conditional is unfortunate

    In hindsight, I am disappointed with the choice of conditional syntax. I know it's too late to change. The problem is

    y = some thing or other if x else something_else

    When scanning this my eye tends to see the first phrase and only later notice that it's conditioned on x (or maybe not notice at all!). Particularly if 'some thing or other' is long or complicated.


  • Aaron \Castironpi\ Brady

    #2
    Re: python syntax for conditional is unfortunate

    On Sep 23, 6:52 pm, Neal Becker <ndbeck...@gmai l.comwrote:
    In hindsight, I am disappointed with the choice of conditional syntax.  I know it's too late to change.  The problem is
    >
    y = some thing or other if x else something_else
    >
    When scanning this my eye tends to see the first phrase and only later notice that it's conditioned on x (or maybe not notice at all!).  Particularly if 'some thing or other' is long or complicated.
    You're talking strictly about readability, which among other things is
    in the eye of the beholder, of course. Temporary variables can clean
    up some code, even if choosing names can be a hassle and it's more
    things to keep track of. Long lines and extra variables form a trade-
    off. You are writing a line with a conditional expression the value
    of which depends on something. What does it depend on, what is its
    value if that's true, and what is it if it's false? '...if...else.. .'
    only takes 6 characters... maybe you want more!

    If you're looking for a strictly syntactic construct, you can always
    "fire blanks", or tracers, if the analogy's more accurate.

    z= conditionally( x ) if b else y

    This could serve as a gentle reminder, even where 'conditionally'
    returns its argument, i.e. is the identity function. You can always
    roll your own ternary with extra parameters too:

    z= condition( b, x, y )

    Just don't confuse it with threading.Condi tion.

    Otherwise, you're stuck with old syntax markers, and unless you
    wanted:

    z= if b then x else y

    You're out of options. You have to express it somehow. Did you want
    the condition first? Was there an alternative proposal you
    preferred? IINM if I'm not mistaken,

    z= b and x or y

    works just the same so long as x evaluates to True, as it will be
    tested.

    Feel free to write your own from scratch, and we'll see how close
    Python can come to resembling it.

    I suppose you can compare it to someone who stops listening before the
    word 'if', and completely misunderstands your statement. "Feed the
    dog if he's standing near the food dish" != "Feed the dog", which can
    of course lead to errors of both omission and commission (doing too
    little -or- too much). There's no way to fix that in a guaranteed
    way, except to say, "listen to the whole statement".

    And strictly sarcastically, what did you want to do with reading the
    program? Why were you reading it? <snicker, ducks>

    Comment

    • namekuseijin

      #3
      Re: python syntax for conditional is unfortunate

      On 23 set, 20:52, Neal Becker <ndbeck...@gmai l.comwrote:
      In hindsight, I am disappointed with the choice of conditional syntax.  I know it's too late to change.  The problem is
      >
      y = some thing or other if x else something_else
      >
      When scanning this my eye tends to see the first phrase and only later notice that it's conditioned on x (or maybe not notice at all!).  Particularly if 'some thing or other' is long or complicated.
      Yes, infix syntax sucks. No ambiguities in prefixed Lisp: (if x (or
      something other) something_else)

      Anyway, pretty amusing seeing Guido nodding to Larry... ;)

      Comment

      • Neal Becker

        #4
        Re: python syntax for conditional is unfortunate

        Aaron "Castironpi " Brady wrote:
        On Sep 23, 6:52 pm, Neal Becker <ndbeck...@gmai l.comwrote:
        >In hindsight, I am disappointed with the choice of conditional syntax. I
        >know it's too late to change. The problem is
        >>
        >y = some thing or other if x else something_else
        >>
        >When scanning this my eye tends to see the first phrase and only later
        >notice that it's conditioned on x (or maybe not notice at all!).
        >Particularly if 'some thing or other' is long or complicated.
        >
        You're talking strictly about readability, which among other things is
        in the eye of the beholder, of course. Temporary variables can clean
        up some code, even if choosing names can be a hassle and it's more
        things to keep track of. Long lines and extra variables form a trade-
        off. You are writing a line with a conditional expression the value
        of which depends on something. What does it depend on, what is its
        value if that's true, and what is it if it's false? '...if...else.. .'
        only takes 6 characters... maybe you want more!
        >
        If you're looking for a strictly syntactic construct, you can always
        "fire blanks", or tracers, if the analogy's more accurate.
        >
        z= conditionally( x ) if b else y
        >
        This could serve as a gentle reminder, even where 'conditionally'
        returns its argument, i.e. is the identity function. You can always
        roll your own ternary with extra parameters too:
        >
        z= condition( b, x, y )
        >
        Just don't confuse it with threading.Condi tion.
        >
        Otherwise, you're stuck with old syntax markers, and unless you
        wanted:
        >
        z= if b then x else y
        >
        You're out of options. You have to express it somehow. Did you want
        the condition first? Was there an alternative proposal you
        preferred? IINM if I'm not mistaken,
        >
        z= b and x or y
        >
        works just the same so long as x evaluates to True, as it will be
        tested.
        >
        Feel free to write your own from scratch, and we'll see how close
        Python can come to resembling it.
        >
        I suppose you can compare it to someone who stops listening before the
        word 'if', and completely misunderstands your statement. "Feed the
        dog if he's standing near the food dish" != "Feed the dog", which can
        of course lead to errors of both omission and commission (doing too
        little -or- too much). There's no way to fix that in a guaranteed
        way, except to say, "listen to the whole statement".
        >
        And strictly sarcastically, what did you want to do with reading the
        program? Why were you reading it? <snicker, ducks>
        I find I'm often tripped up by:

        x = Y (lots of constructor arguments....) if something ...

        on first glance, I don't notice the if.

        Why am I reading this? Umm, because I wrote it.

        Comment

        • Aaron \Castironpi\ Brady

          #5
          Re: python syntax for conditional is unfortunate

          On Sep 23, 8:50 pm, Neal Becker <ndbeck...@gmai l.comwrote:
          Aaron "Castironpi " Brady wrote:
          On Sep 23, 6:52 pm, Neal Becker <ndbeck...@gmai l.comwrote:
          In hindsight, I am disappointed with the choice of conditional syntax. I
          know it's too late to change.  The problem is
          >
          y = some thing or other if x else something_else
          >
          When scanning this my eye tends to see the first phrase and only later
          notice that it's conditioned on x (or maybe not notice at all!).
          Particularly if 'some thing or other' is long or complicated.
          >
          You're talking strictly about readability, which among other things is
          in the eye of the beholder, of course.  Temporary variables can clean
          up some code, even if choosing names can be a hassle and it's more
          things to keep track of.  Long lines and extra variables form a trade-
          off.  You are writing a line with a conditional expression the value
          of which depends on something.  What does it depend on, what is its
          value if that's true, and what is it if it's false?  '...if...else.. .'
          only takes 6 characters... maybe you want more!
          >
          If you're looking for a strictly syntactic construct, you can always
          "fire blanks", or tracers, if the analogy's more accurate.
          >
          z= conditionally( x ) if b else y
          >
          This could serve as a gentle reminder, even where 'conditionally'
          returns its argument, i.e. is the identity function.  You can always
          roll your own ternary with extra parameters too:
          >
          z= condition( b, x, y )
          >
          Just don't confuse it with threading.Condi tion.
          >
          Otherwise, you're stuck with old syntax markers, and unless you
          wanted:
          >
          z= if b then x else y
          >
          You're out of options.  You have to express it somehow.  Did you want
          the condition first?  Was there an alternative proposal you
          preferred?  IINM if I'm not mistaken,
          >
          z= b and x or y
          >
          works just the same so long as x evaluates to True, as it will be
          tested.
          >
          Feel free to write your own from scratch, and we'll see how close
          Python can come to resembling it.
          >
          I suppose you can compare it to someone who stops listening before the
          word 'if', and completely misunderstands your statement.  "Feed the
          dog if he's standing near the food dish" != "Feed the dog", which can
          of course lead to errors of both omission and commission (doing too
          little -or- too much).  There's no way to fix that in a guaranteed
          way, except to say, "listen to the whole statement".
          >
          And strictly sarcastically, what did you want to do with reading the
          program?  Why were you reading it?  <snicker, ducks>
          >
          I find I'm often tripped up by:
          >
          x = Y (lots of  constructor arguments....) if something ...
          >
          on first glance, I don't notice the if.
          >
          Why am I reading this?  Umm, because I wrote it.
          Put it somewhere you'll notice!

          Comment

          • namekuseijin

            #6
            Re: python syntax for conditional is unfortunate

            On 23 set, 22:50, Neal Becker <ndbeck...@gmai l.comwrote:
            I find I'm often tripped up by:
            >
            x = Y (lots of  constructor arguments....) if something ...
            >
            on first glance, I don't notice the if.
            Nobody does. This peculiar syntax has much better usage in short
            expressions. dothis if this else dothat

            Comment

            • Roy Smith

              #7
              Re: python syntax for conditional is unfortunate

              "Aaron \"Castironpi \" Brady" <castironpi@gma il.comwrote:
              You're out of options. You have to express it somehow.
              How about:

              Assignith z the value of x if the value of b is such that it is true, else
              assignith it the value of y. Assignith z not the value of w, nor the value
              of v, lest you raise NameError upon thy stack trace.

              Comment

              • Bruno Desthuilliers

                #8
                Re: python syntax for conditional is unfortunate

                Neal Becker a écrit :
                (snip)
                I find I'm often tripped up by:
                >
                x = Y (lots of constructor arguments....) if something ...
                >
                on first glance, I don't notice the if.
                Indeed. The inline conditionnal syntax is obviously innappropriate here.
                It's just like list-comprehensions : just fine for simple use case,
                definitively not appropriate when it comes to anything complex. IOW :
                it's not the syntax that's unfortunate, it's the coding style that's wrong.

                My 2 cents...

                Comment

                • Asun Friere

                  #9
                  Re: python syntax for conditional is unfortunate

                  On Sep 24, 9:52 am, Neal Becker <ndbeck...@gmai l.comwrote:
                  In hindsight, I am disappointed with the choice of conditional syntax. I know it's too late to change. The problem is
                  >
                  y = some thing or other if x else something_else
                  >
                  When scanning this my eye tends to see the first phrase and only later notice that it's conditioned on x (or maybe not notice at all!). Particularly if 'some thing or other' is long or complicated.
                  The struggle to get a conditional operator was a long and bitter, so
                  in the first place we should be glad we aren't writing "y =
                  (conditional and [p] or [q])[0] anymore. Since it was but grudgingly
                  bestowed I thought BDFL had chosen this particular syntax just to be
                  difficult.

                  However since using it for a while, I am surprised how natural it is
                  to use and read. A canonical use of the conditional operator is in
                  pluralising words, (eg. '%s dollar' % n + 's' if n!=1 else ''). For
                  this and similar short uses, where the regular if statement is an
                  annoyance this syntax <ususal_casei f <conditionsel se
                  <unusual_casewo rks nicely. More complicated conditionals or cases
                  are probably better handled by an if statement. This syntax is also
                  probably not the best for nested conditionals. The latter, however,
                  is probably feature rather than bug.

                  Comment

                  • Pete Forman

                    #10
                    Re: python syntax for conditional is unfortunate

                    Asun Friere <afriere@yahoo. co.ukwrites:
                    A canonical use of the conditional operator is in
                    pluralising words, (eg. '%s dollar' % n + 's' if n!=1 else '').
                    That fails for n == 1. So what is best?

                    for i in range(4):
                    print '%d thing' % i + ('s' if i != 1 else '')

                    for i in range(4):
                    print '%d thing%s' % (i, ('s', '')[i==1])

                    for i in range(4):
                    print '%d thing%s' % (i, 's' if i != 1 else '')


                    --
                    Pete Forman -./\.- Disclaimer: This post is originated
                    WesternGeco -./\.- by myself and does not represent
                    pete.forman@wes terngeco.com -./\.- the opinion of Schlumberger or
                    http://petef.22web.net -./\.- WesternGeco.

                    Comment

                    • Asun Friere

                      #11
                      Re: python syntax for conditional is unfortunate

                      On Sep 25, 3:16 am, Pete Forman <pete.for...@we sterngeco.comwr ote:
                      Asun Friere <afri...@yahoo. co.ukwrites:
                      >
                      A canonical use of the conditional operator is in
                      pluralising words, (eg. '%s dollar' % n + 's' if n!=1 else '').
                      >
                      That fails for n == 1. So what is best?
                      >
                      Sorry missing parentheses. I should test before posting, even for
                      code written into.
                      for i in range(4):
                      print '%d thing' % i + ('s' if i != 1 else '')
                      That is the correct version of what I meant, but your last, including
                      all variables for placeholders in the tuple is probably better.

                      Comment

                      • Asun Friere

                        #12
                        Re: python syntax for conditional is unfortunate

                        On Sep 25, 3:16 am, Pete Forman <pete.for...@we sterngeco.comwr ote:
                        Asun Friere <afri...@yahoo. co.ukwrites:
                        >
                        A canonical use of the conditional operator is in
                        pluralising words, (eg. '%s dollar' % n + 's' if n!=1 else '').
                        >
                        That fails for n == 1. So what is best?
                        >
                        Sorry missing parantheses. I should test, even for fragments written
                        out as part of a sentence. %-/
                        for i in range(4):
                        print '%d thing' % i + ('s' if i != 1 else '')
                        >
                        That's the corrected version of what I meant, but actually I think
                        your last version ('%d thing%s' % (i, 's' if i != 1 else '')), holding
                        all variables for placeholders in the tuple, is better. It's certainly
                        more readible.

                        Comment

                        • Aaron \Castironpi\ Brady

                          #13
                          Re: python syntax for conditional is unfortunate

                          On Sep 24, 8:40 pm, Asun Friere <afri...@yahoo. co.ukwrote:
                          On Sep 25, 3:16 am, Pete Forman <pete.for...@we sterngeco.comwr ote:
                          >
                          Asun Friere <afri...@yahoo. co.ukwrites:
                          >
                           A canonical use of the conditional operator is in
                           pluralising words, (eg. '%s dollar' % n + 's' if n!=1 else '').
                          >
                          That fails for n == 1.  So what is best?
                          >
                          Sorry missing parantheses.  I should test, even for fragments written
                          out as part of a sentence. %-/
                          >
                          for i in range(4):
                              print '%d thing' % i + ('s' if i != 1 else '')
                          >
                          That's the corrected version of what I meant, but actually I think
                          your last version ('%d thing%s' % (i, 's' if i != 1 else '')), holding
                          all variables for placeholders in the tuple, is better. It's certainly
                          more readible.
                          It's a different answer if you have 'things is/are'. '%d thing%s %s'%
                          ( ( i, )+ ( 's', 'are' ) if i!= 1 else ( '', 'is' ) ). Or excluding
                          prepositional phrases and subordinate clauses, '%d thing%s'% ( i, 's
                          are' if i!= 1 else ' is' ).

                          Comment

                          • Asun Friere

                            #14
                            Re: python syntax for conditional is unfortunate

                            On Sep 25, 11:57 am, "Aaron \"Castironpi \" Brady"
                            <castiro...@gma il.comwrote:
                            On Sep 24, 8:40 pm, Asun Friere <afri...@yahoo. co.ukwrote:
                            ... I think
                            your last version ('%d thing%s' % (i, 's' if i != 1 else '')), holding
                            all variables for placeholders in the tuple, is better. It's certainly
                            more readible.
                            >
                            It's a different answer if you have 'things is/are'. '%d thing%s %s'%
                            ( ( i, )+ ( 's', 'are' ) if i!= 1 else ( '', 'is' ) ). Or excluding
                            prepositional phrases and subordinate clauses, '%d thing%s'% ( i, 's
                            are' if i!= 1 else ' is' ).
                            Forgive me for being dull, my caffeine levels have not yet optimal,
                            but I don't follow. Both the solutions you propose do put all the
                            placeholder variables in the tuple. Or are you saying it no longer
                            remains readible?

                            BTW you repeated my mistake with the first scraplet of code.

                            Comment

                            Working...