Order of evaluation in conditionals

Discussion in 'Python' started by Karlo Lozovina, Feb 25, 2008.

  1. Hi all! Here is what's bugging me: let's say I have this code:

    if (<condition1>) and (<condition2>) and (<condition3>):
    do_something()

    Is there a guarantee that Python will evaluate those conditions in order (1,
    2, 3)? I know I can write that as a nested if, and avoid the problem
    altogether, but now I'm curious about this ;).

    Thanks...

    --
    Karlo Lozovina -- Mosor
     
    Karlo Lozovina, Feb 25, 2008
    #1
    1. Advertising

  2. Karlo Lozovina

    Paul Hankin Guest

    On Feb 25, 9:18 pm, Karlo Lozovina <> wrote:
    > Hi all! Here is what's bugging me: let's say I have this code:
    >
    > if (<condition1>) and (<condition2>) and (<condition3>):
    >   do_something()
    >
    > Is there a guarantee that Python will evaluate those conditions in order (1,
    > 2, 3)? I know I can write that as a nested if, and avoid the problem
    > altogether, but now I'm curious about this ;).


    Did you try to find the answer to your question in the python
    reference manual? The relevant page is http://docs.python.org/ref/Booleans.html

    To quote it:
    The expression 'x and y' first evaluates x; if x is false, its value
    is returned; otherwise, y is evaluated and the resulting value is
    returned.

    --
    Paul Hankin
     
    Paul Hankin, Feb 25, 2008
    #2
    1. Advertising

  3. Karlo Lozovina

    Tim Chase Guest

    > if (<condition1>) and (<condition2>) and (<condition3>):
    > do_something()
    >
    > Is there a guarantee that Python will evaluate those conditions in order (1,
    > 2, 3)? I know I can write that as a nested if, and avoid the problem
    > altogether, but now I'm curious about this ;).


    Yes, Python does short-circuit evaluation, from left-to-right

    http://docs.python.org/tut/node7.html#SECTION007700000000000000000

    That means that if <condition1> returns false, condition[2|3]
    will not be evaluated (and similarly, if condition2 returns
    false, condition3 won't be evaluated).

    -tkc
     
    Tim Chase, Feb 25, 2008
    #3
  4. Paul Hankin wrote:

    > Did you try to find the answer to your question in the python
    > reference manual? The relevant page is
    > http://docs.python.org/ref/Booleans.html


    Of course I first Googled (even Google Groups-ed) it, but I didn't notice
    that in the results.

    > To quote it:
    > The expression 'x and y' first evaluates x; if x is false, its value
    > is returned; otherwise, y is evaluated and the resulting value is
    > returned.


    Thanks (to both of you :>), that's it.

    --
    Karlo Lozovina -- Mosor
     
    Karlo Lozovina, Feb 25, 2008
    #4
    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. Nan Li

    evaluation order

    Nan Li, Nov 14, 2005, in forum: Java
    Replies:
    11
    Views:
    660
    Oliver Wong
    Nov 15, 2005
  2. Xavier Decoret

    Evaluation order for a=b

    Xavier Decoret, Jul 3, 2003, in forum: C++
    Replies:
    1
    Views:
    348
    Ron Natalie
    Jul 3, 2003
  3. Ilias Lazaridis
    Replies:
    2
    Views:
    401
    Ilias Lazaridis
    Apr 24, 2005
  4. Ilias Lazaridis
    Replies:
    74
    Views:
    791
    Ilias Lazaridis
    Apr 4, 2005
  5. Ilias Lazaridis
    Replies:
    18
    Views:
    359
    Bill Guindon
    Apr 9, 2005
Loading...

Share This Page