Most compact "X if X else Y" idiom

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • jbperez808@yahoo.com

    #1

    Most compact "X if X else Y" idiom

    I find myself having to do the following:

    x = (some complex expression)
    y = x if x else "blah"

    and I was wondering if there is any built-in idiom that
    can remove the need to put (some complex expression)
    in the temporary variable x.

    e.g. something like the below:

    y= foobar ((some complex expression), "blah")

    I realized foobar() can be easily coded as:
    def foobar(a,b):
    if a: return a
    else: return b

    But I was wondering if there was a built-in function or syntax
    that already does this.
  • Aaron \Castironpi\ Brady

    #2
    Re: Most compact "X if X else Y" idiom

    On Oct 12, 12:01 am, jbperez...@yaho o.com wrote:
    I find myself having to do the following:
    >
      x = (some complex expression)
      y = x if x else "blah"
    >
    and I was wondering if there is any built-in idiom that
    can remove the need to put (some complex expression)
    in the temporary variable x.
    >
    e.g. something like the below:
    >
     y= foobar ((some complex expression), "blah")
    >
    I realized foobar() can be easily coded as:
      def foobar(a,b):
        if a: return a
        else: return b
    >
    But I was wondering if there was a built-in function or syntax
    that already does this.
    You could take your chances on 'or', as follows:
    >>(6+ (3<< 1) ) or 'blah'
    12
    >>(6- (3<< 1) ) or 'blah'
    'blah'

    You don't need to use the ternary statement:

    y = (some complex expression)
    if not y:
    y = "blah"

    If you find yourself using it a lot, why not add it to your site's
    utilities modules? Take your time, and if you find numerous uses,
    present them and make the case Python should have a built-in to do it,
    something like 'ditto' marks:

    (6- (3<< 1) ) if ditto else 'blah'

    Comment

    • Terry Reedy

      #3
      Re: Most compact &quot;X if X else Y&quot; idiom

      jbperez808@yaho o.com wrote:
      I find myself having to do the following:
      >
      x = (some complex expression)
      y = x if x else "blah"
      >
      and I was wondering if there is any built-in idiom that
      can remove the need to put (some complex expression)
      in the temporary variable x.
      A common idiom for this particular case where the if-expression is also
      the conditional or the basic of the conditional expression is

      y = <some complex expression>
      if not y: y = "blah"

      Comment

      • Steven D'Aprano

        #4
        Re: Most compact &quot;X if X else Y&quot; idiom

        On Sat, 11 Oct 2008 22:01:46 -0700, jbperez808 wrote:
        I find myself having to do the following:
        >
        x = (some complex expression)
        y = x if x else "blah"
        >
        and I was wondering if there is any built-in idiom that can remove the
        need to put (some complex expression) in the temporary variable x.
        Use short-circuit Booleans:

        y = x or "blah"

        If x is any true value (non-zero number, non-empty string etc.) then y
        will be set to x; but if x is any false value (zero, empty string, None,
        empty list, etc.) then y will be set to "blah".


        However, this technique doesn't work for arbitrary tests. For example,
        you can't simplify the following:

        x = (some complex expression)
        y = x if 100<=x<250 else "blah"

        (at least I can't think of any way).





        --
        Steven

        Comment

        • Steven D'Aprano

          #5
          Re: Most compact &quot;X if X else Y&quot; idiom

          On Sun, 12 Oct 2008 05:30:33 +0000, Steven D'Aprano wrote:
          Use short-circuit Booleans:
          >
          y = x or "blah"
          Except of course you don't use x, you use the complex expression.

          y = (some complex expression) or "blah"



          Sorry for the itchy posting finger.



          --
          Steven

          Comment

          • jbperez808@yahoo.com

            #6
            Re: Most compact &quot;X if X else Y&quot; idiom

            Thanks, folks.

            Short-circuit boolean was the syntax I had in mind which
            momentarily escaped me, but the "if not x: x='blah'" idiom
            was instructive as well.

            Comment

            Working...