[Python-Dev] Re: Trinary Operators

Gerald S. Williams gsw@agere.com
Thu, 6 Feb 2003 16:18:43 -0500


Guido van Rossum wrote:
> > For example, with regard to short-circuiting, it will be
> > inconsistent with other expressions at some level in
> > either form or function.
> 
> How so?
[ ... ]
>   if x and y:
>      print 1
>   else
>      print 0
> 
> means the same as
> 
>   print 1 if x and y else 0

That's what I figured.

In that case, the "inconsistency in form" is the order of
evaluation. The expression in the middle is evaluated,
then either the one on the left or the one on the right.
Not everyone will see this as a big issue, but it is a
potential source of confusion. I did qualify my statement
with "at some level". :-)

Compare this to the order of evaluation in the current
equivalent (parentheses for clarity only):
   (x and y) and 1 or 0

or if you prefer:
   ((x and y) and [1] or [0])[0]

The order of evaluation is the same, but the form is
different. If you are expecting short-circuiting, it's
clear in this case that it's left-to-right.

-Jerry