<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
<html>
"Colin J. Williams" wrote:
<blockquote TYPE=CITE>Stephen Horne wrote:
<blockquote TYPE=CITE>On Fri, 14 Feb 2003 22:25:23 -0800, Erik Max Francis
<max@alcyone.com>
<br>wrote:
<p>>Stephen Horne wrote:
<br>>
<br>>> The '... if ... else ...' version seems to have a natural
<br>>> repetition-based pattern of growth which need not rely on obvious
<br>>> nesting...
<br>>>
<br>>>   x if c1 else y if c2 else z
<br>>>
<br>>> OK, yes, it is nesting really - but, like the C 'else if', it reads
<br>>> more like a continuation of the same thing than a nesting of one
thing
<br>>> in another.
<br>>
<br>>I really don't follow.  I would argue exactly the opposite: 
Since
<br>>if/else doesn't read left-to-right, the use of it for nesting (they're
<br>>nesting both ways, however you want to look at it) is _more_
<br>>strightforward on the strict left-to-right ordering of then/else than
on
<br>>the center-left-right one.  In the center-left-right one, it
is harder
<br>>to tell which condition belongs to what expressions due to the out
of
<br>>order pattern.
<p>I actually kind of agree with you that 'x if c then y' seems a little
<br>odd, even now that I've got used to it a bit more. But I don't really
<br>agree with you on this.
<p>You seem to be preoccupied with the order of evaluation - the fact
<br>that the condition must be evaluated first, whereas only one of the
<br>values to select from is evaluated at all.
<p>I don't see things that way. I have more of a functional view of
<br>expressions. In general, I think I shouldn't need to worry about the
<br>order of evaluation - only about the meaning and the readability. In
<br>expressions, evaluation order only makes a difference to the meaning
<br>if there are side effects (except in that lazy evaluation can ignore
<br>subexpressions which are not needed, but which might otherwise trigger
<br>errors). Having functions with side effects is often considered bad
<br>style, partly for this exact reason.
<br> </blockquote>
Apologies to the many who have followed this thread a lot more closely
<br>than I have, if this matter has been explored before.
<p>I suggest that acount should be taken of evaluation for the reason given
<br>above.  Having side effects might be bad style, but there is nothing
to
<br>prevent the use of this bad style.
<p>Typically, expressions are evaluated from left to right, with variation
<br>for operator precedence.  Thus, I suggest that some scheme which
<br>puts the condition to the left of the expression is desirable, so that
<br>the human reader can see the flow of evaluation.
<p>The C scheme using ? achieves this, but is probably over cryptic
<br>for the newcomer to Python.
<p>There has been such an overwhelming response on this matter
<br>that it would be helpful if someone could post a list of the options
<br>on the table, perhaps with a brief evaluation of each.
<p>Colin W.</blockquote>
PS  The simplest scheme, suggested by others, which keeps
<br>the condition on the left is probably:
<p>    if (cond, choiceT, choiceF)
<p>where "if" is a function defined as:
<p>def if(cond= None, choiceT, choiceF):
<br>  # The cond is always evaluated first,
<br>  # the choice which is not made is not evaluated
<br>  if cond is None:
<br>    raise ValueError
<br>  elif cond:
<br>    return choiceT
<br>  else:
<br>    return choiceF</html>