if in expression

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • christian2.schmidt@gmx.de

    if in expression

    Hi,
    I'm using IronPython to evaluate expressions, so I can't use the
    return statement but have to say it in an one-liner. Using C/C++ I
    could use "cond ? then : else" to have an expression-if, but in Python
    there's no such operator. The "cond and then or else"-trick only seems
    to work for non-false "then"s and is not very readable.
    So is the pythonic way to def iif(cond, then, else_): if cond: return
    then; else: else_; ? But actually this is not the same, because "then"
    and "else_" are evaluated independently of cond...

    What is the right way to have if in an expression?

    Thanks,
    Christian
  • Diez B. Roggisch

    #2
    Re: if in expression

    christian2.schm idt@gmx.de schrieb:
    Hi,
    I'm using IronPython to evaluate expressions, so I can't use the
    return statement but have to say it in an one-liner. Using C/C++ I
    could use "cond ? then : else" to have an expression-if, but in Python
    there's no such operator. The "cond and then or else"-trick only seems
    to work for non-false "then"s and is not very readable.
    So is the pythonic way to def iif(cond, then, else_): if cond: return
    then; else: else_; ? But actually this is not the same, because "then"
    and "else_" are evaluated independently of cond...
    >
    What is the right way to have if in an expression?
    Since python 2.5, it is

    <then_valueif <condelse <else_value>

    If you want lazy evaluation, you can use lambdas:

    iif(cond, lambda: then, lambda: else_)()

    Diez

    Comment

    • John Machin

      #3
      Re: if in expression

      On Aug 18, 5:46 pm, christian2.schm ...@gmx.de wrote:
      Hi,
      I'm using IronPython to evaluate expressions, so I can't use the
      return statement but have to say it in an one-liner.
      By "evaluate expressions", do you mean using the eval built-in
      function? If so, find the recent thread addressing this topic; it was
      asserted that 98% of eval() use-cases were better implemented another
      way. What's _your_ use-case?
      Using C/C++ I
      could use "cond ? then : else" to have an expression-if, but in Python
      there's no such operator. The "cond and then or else"-trick only seems
      to work for non-false "then"s and is not very readable.
      There is an even more unreadable hack (due to Tim Peters IIRC) that
      avoides the false-then problem:

      (cond and [then_value] or [else_value])[0]

      Comment

      • Hrvoje Niksic

        #4
        Re: if in expression

        "Diez B. Roggisch" <deets@nospam.w eb.dewrites:
        Since python 2.5, it is
        >
        <then_valueif <condelse <else_value>
        >
        If you want lazy evaluation, you can use lambdas:
        >
        iif(cond, lambda: then, lambda: else_)()
        Your code uses "iif" and attempts to evaluate a tuple; could you post
        an example that works?

        I ask because it's not clear what you mean by lazy evaluation in this
        context. The ternary "if" expression introduced in Python 2.5 only
        evaluates then_value or else_value depending on the outcome of cond.
        How is that different than using a lambda?

        Comment

        • Fredrik Lundh

          #5
          Re: if in expression

          John Machin wrote:
          There is an even more unreadable hack (due to Tim Peters IIRC) that
          avoides the false-then problem:
          >
          (cond and [then_value] or [else_value])[0]
          The correct attribution is "due to Tim Peters (who wishes it was Steve
          Majewski)."

          </F>

          Comment

          • Diez B. Roggisch

            #6
            Re: if in expression

            Hrvoje Niksic schrieb:
            "Diez B. Roggisch" <deets@nospam.w eb.dewrites:
            >
            >Since python 2.5, it is
            >>
            ><then_valuei f <condelse <else_value>
            >>
            >If you want lazy evaluation, you can use lambdas:
            >>
            >iif(cond, lambda: then, lambda: else_)()
            >
            Your code uses "iif" and attempts to evaluate a tuple; could you post
            an example that works?
            I ask because it's not clear what you mean by lazy evaluation in this
            context. The ternary "if" expression introduced in Python 2.5 only
            evaluates then_value or else_value depending on the outcome of cond.
            How is that different than using a lambda?

            If iif is defined as this:

            def iif(cond, then, else_):
            if cond:
            return then
            else:
            return else_

            you have the problem that "then" and "else_" get evaluated *before* they
            get passed. So for example this factorial function will fail with too
            deep recursion error:

            def f(n):
            return iif(n1, n * f(n-1), 1)

            But if you do it like this, the then and else_ will be functions that
            get returned and then they need to be called to be acutally evaluated:

            def f(n):
            return iif(n1, lambda: n * f(n-1), lambda: 1)()


            Diez

            Comment

            • Fredrik Lundh

              #7
              Re: if in expression

              Hrvoje Niksic wrote:
              >If you want lazy evaluation, you can use lambdas:
              >>
              >iif(cond, lambda: then, lambda: else_)()
              >
              Your code uses "iif" and attempts to evaluate a tuple; could you post
              an example that works?
              >
              I ask because it's not clear what you mean by lazy evaluation in this
              context. The ternary "if" expression introduced in Python 2.5 only
              evaluates then_value or else_value depending on the outcome of cond.
              How is that different than using a lambda?
              the second part of Diez' answer is a reply to the second part of the
              OP's question.

              </F>

              Comment

              • Hrvoje Niksic

                #8
                Re: if in expression

                Fredrik Lundh <fredrik@python ware.comwrites:
                Hrvoje Niksic wrote:
                >
                >>If you want lazy evaluation, you can use lambdas:
                >>>
                >>iif(cond, lambda: then, lambda: else_)()
                >>
                >Your code uses "iif" and attempts to evaluate a tuple; could you post
                >an example that works?
                >>
                >I ask because it's not clear what you mean by lazy evaluation in this
                >context. The ternary "if" expression introduced in Python 2.5 only
                >evaluates then_value or else_value depending on the outcome of cond.
                >How is that different than using a lambda?
                >
                the second part of Diez' answer is a reply to the second part of the
                OP's question.
                Ah, that explains it. Sorry about the noise!

                Comment

                • Christian Schmidt

                  #9
                  Re: if in expression

                  On 18 Aug., 10:22, John Machin <sjmac...@lexic on.netwrote:
                  On Aug 18, 5:46 pm, christian2.schm ...@gmx.de wrote:
                  I'm using IronPython to evaluate expressions, so I can't use the
                  return statement but have to say it in an one-liner.
                  >
                  By "evaluate expressions", do you mean using the eval built-in
                  function?Ifso, find the recent thread addressing this topic; it was
                  asserted that 98% of eval() use-cases were better implemented another
                  way. What's _your_ use-case?
                  User provided python expression of .net's CLR-objects (with
                  overloaded
                  operators). The result is processed again in .net.
                  Reading through the mentioned mail, I think I'm in the 2%. :-)

                  Thanks,
                  Christian

                  Comment

                  Working...