if does not evaluate

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jim Newton

    if does not evaluate

    A question that has bothered me ever since looking at python
    the first time is that not everything evaluates to something.
    I am wondering what is the reason for this. Would making
    everying evaluate have caused the langugage to be less
    efficient execution-wise? or was it a choice to enforce some
    sort of standard?

    I've read a few discussions about the fact that there is
    no if/then/else operartor.

    Wouldn't this problem be easily solved by making the built
    in keywords actually be evaluatable.

    I.e., x = if something:
    expr1
    else:
    expr2

    parentheses would of course be optional as they are for
    all expressions.

  • bruno modulix

    #2
    Re: if does not evaluate

    Jim Newton a écrit :[color=blue]
    > A question that has bothered me ever since looking at python
    > the first time is that not everything evaluates to something.
    > I am wondering what is the reason for this. Would making
    > everying evaluate have caused the langugage to be less
    > efficient execution-wise? or was it a choice to enforce some
    > sort of standard?
    >
    > I've read a few discussions about the fact that there is
    > no if/then/else operartor.
    >
    > Wouldn't this problem be easily solved by making the built
    > in keywords actually be evaluatable.
    >
    > I.e., x = if something:
    > expr1
    > else:
    > expr2
    >
    > parentheses would of course be optional as they are for
    > all expressions.
    >[/color]
    Python is, by design, an 'instruction-based' language, not an
    'expression-based' language. Search this ng for discussions on the
    inclusion of ternary operator if you want to learn more about this point...

    Now if you're looking for a language pretty similar to Python
    (very-hi-level, highly dynamic, interpreted, clean and readable syntax,
    friendly and enthusiast communauty etc) where everything's an
    expression, you may want to try Ruby. It's not as easy to learn, it as
    its own warts too, and it has not yet reached such a 'critical mass' as
    Python in terms of libs and communauty, but it's a really nice and
    usable language too.

    Bruno (who likes both languages...)

    Comment

    • Tor Iver Wilhelmsen

      #3
      Re: if does not evaluate

      Jim Newton <jimka@rdrop.co m> writes:
      [color=blue]
      > I.e., x = if something:
      > expr1
      > else:
      > expr2[/color]

      Try exploiting that a boolean expression evaluates to 0 or 1:

      x = (expr1, expr2)[something];

      Keep in mind that this might break on some later release of Python if
      they decide to make boolean its own type, but at least it works for
      2.3.3.

      Comment

      • Tor Iver Wilhelmsen

        #4
        Re: if does not evaluate

        Tor Iver Wilhelmsen <tor.iver.wilhe lmsen@broadpark .no> writes:
        [color=blue]
        > x = (expr1, expr2)[something];[/color]

        Um, other way around of course :) :

        x = (expr2, expr1)[something];

        Comment

        • Matteo Dell'Amico

          #5
          Re: if does not evaluate

          Tor Iver Wilhelmsen wrote:[color=blue]
          > x = (expr1, expr2)[something];
          >
          > Keep in mind that this might break on some later release of Python if
          > they decide to make boolean its own type, but at least it works for
          > 2.3.3.[/color]

          I think (and I hope) it will never be so, since bool is already its own
          type and, as the documentation states,

          "In numeric contexts (for example when used as the argument to an
          arithmetic operator), they behave like the integers 0 and 1, respectively."

          So, even if this a little "perlish" :-), I think it will continue to
          work in future versions of python, since otherwise it would break too
          much existing code.

          --
          Ciao,
          Matteo

          Comment

          • Holger Türk

            #6
            Re: if does not evaluate



            Tor Iver Wilhelmsen wrote:[color=blue]
            > Jim Newton <jimka@rdrop.co m> writes:
            >
            >[color=green]
            >>I.e., x = if something:
            >> expr1
            >> else:
            >> expr2[/color]
            >
            >
            > Try exploiting that a boolean expression evaluates to 0 or 1:
            >
            > x = (expr1, expr2)[something];[/color]

            This is eagerly evaluated. If only one expression is meant to
            be evaluated, the whole thing should look like this:

            x = (lambda: expr1, lambda: expr2)[bool(something)]()

            I added bool() to allow tests returning other values than
            0 or 1.

            Greetings,

            Holger

            Comment

            • Tor Iver Wilhelmsen

              #7
              Re: if does not evaluate

              Matteo Dell'Amico <della@toglimi. linux.it> writes:
              [color=blue]
              > So, even if this a little "perlish" :-), I think it will continue to
              > work in future versions of python, since otherwise it would break too
              > much existing code.[/color]

              Yes, and at least one would hope it would work anyway with a cast,
              that is "int(something) ".

              Comment

              • Jim Newton

                #8
                Re: if does not evaluate

                this suggestion does not work because
                expr2 evaluates even if expr1 is TRUE.


                Tor Iver Wilhelmsen wrote:[color=blue]
                > Jim Newton <jimka@rdrop.co m> writes:
                >
                >[color=green]
                >>I.e., x = if something:
                >> expr1
                >> else:
                >> expr2[/color]
                >
                >
                > Try exploiting that a boolean expression evaluates to 0 or 1:
                >
                > x = (expr1, expr2)[something];
                >
                > Keep in mind that this might break on some later release of Python if
                > they decide to make boolean its own type, but at least it works for
                > 2.3.3.[/color]

                Comment

                • Tor Iver Wilhelmsen

                  #9
                  Re: if does not evaluate

                  Jim Newton <jimka@rdrop.co m> writes:
                  [color=blue]
                  > this suggestion does not work because
                  > expr2 evaluates even if expr1 is TRUE.[/color]

                  Yes, someone pointed that out already :) - the solution was to use
                  lambda expressions, which is reasonable.

                  Comment

                  Working...