if does not evaluate

Discussion in 'Python' started by Jim Newton, Jun 5, 2004.

  1. Jim Newton

    Jim Newton Guest

    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.
    Jim Newton, Jun 5, 2004
    #1
    1. Advertising

  2. Jim Newton a écrit :
    > 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.
    >

    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...)
    bruno modulix, Jun 5, 2004
    #2
    1. Advertising

  3. Jim Newton <> writes:

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


    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.
    Tor Iver Wilhelmsen, Jun 6, 2004
    #3
  4. Tor Iver Wilhelmsen <> writes:

    > x = (expr1, expr2)[something];


    Um, other way around of course :) :

    x = (expr2, expr1)[something];
    Tor Iver Wilhelmsen, Jun 6, 2004
    #4
  5. Tor Iver Wilhelmsen wrote:
    > 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.


    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
    Matteo Dell'Amico, Jun 6, 2004
    #5
  6. Tor Iver Wilhelmsen wrote:
    > Jim Newton <> writes:
    >
    >
    >>I.e., x = if something:
    >> expr1
    >> else:
    >> expr2

    >
    >
    > Try exploiting that a boolean expression evaluates to 0 or 1:
    >
    > x = (expr1, expr2)[something];


    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
    =?ISO-8859-1?Q?Holger_T=FCrk?=, Jun 6, 2004
    #6
  7. Matteo Dell'Amico <> writes:

    > 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.


    Yes, and at least one would hope it would work anyway with a cast,
    that is "int(something)".
    Tor Iver Wilhelmsen, Jun 6, 2004
    #7
  8. Jim Newton

    Jim Newton Guest

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


    Tor Iver Wilhelmsen wrote:
    > Jim Newton <> writes:
    >
    >
    >>I.e., x = if something:
    >> expr1
    >> else:
    >> expr2

    >
    >
    > 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.
    Jim Newton, Jun 7, 2004
    #8
  9. Jim Newton <> writes:

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


    Yes, someone pointed that out already :) - the solution was to use
    lambda expressions, which is reasonable.
    Tor Iver Wilhelmsen, Jun 8, 2004
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rick Osborn
    Replies:
    10
    Views:
    3,951
    Jon A. Cruz
    Feb 8, 2004
  2. kushalsoftpro
    Replies:
    6
    Views:
    2,181
    John Harrison
    Nov 4, 2004
  3. Robert Brewer

    RE: if does not evaluate

    Robert Brewer, Jun 5, 2004, in forum: Python
    Replies:
    33
    Views:
    913
    Jacek Generowicz
    Jun 17, 2004
  4. Abhi
    Replies:
    7
    Views:
    2,477
  5. KevinSimonson
    Replies:
    6
    Views:
    833
    Bo Persson
    Oct 23, 2010
Loading...

Share This Page